Author Topic: A typical Connect 4 game in TI-Basic  (Read 6251 times)

0 Members and 1 Guest are viewing this topic.

_player1537

  • Guest
Re: A typical Connect 4 game in TI-Basic
« Reply #15 on: March 13, 2010, 05:32:08 pm »
OK, here goes.  I would like to point out that checking the board doesn't fully work yet, but it shouldn't be too hard to fix.  I might get around to fixing it, just don't run it with checking enabled.  The AI does work as expected though.  Sorry if my code is hard to understand, I'll explain it if you want.

Edit: This really does need an explanation...
Prgm CheckBrd starts at a certain spot and checks in the direction you specify (look in connect AI where it has the list then runs the program) it keeps going that direction until it meets a different tile, counting as it goes.  When it reaches a different tile it goes back to the starting spot and keeps counting in the opposite direction.  I just realized that I could implement your method for horizontal and vertical checking.  Anyways, there you go ;)


CheckBrd:
Code: [Select]
:{Ans(1),Ans(2),Ans(3),Ans(4),Ans(5),Ans(6),Ans(7),1
:Repeat Ans(7)=2 or Ans(6)≥4
:If max(Ans(1)+Ans(3)={1,2,3,4,5,6}) and max(Ans(2)+Ans(4)={1,2,3,4,5,6,7})
:Then
:If [A](Ans(1)+Ans(3),Ans(2)+Ans(4))=Ans(5)
:Then
:{Ans(1)+Ans(3),Ans(2)+Ans(4),Ans(3),Ans(4),Ans(5),Ans(6)+1,Ans(7),Ans(8
:Else
:{C,A,‾Ans(3),‾Ans(4),Ans(5),Ans(6),Ans(7)+1,Ans(8
:End
:Else
:{C,A,‾Ans(3),‾Ans(4),Ans(5),Ans(6),Ans(7)+1,Ans(8
:
:End
:If Ans(6)≥Ans(8
:Then
:For(E,A+.1C,‾10
:End
:{Ans(1),Ans(2),Ans(3),Ans(4),Ans(5),Ans(6),Ans(7),Ans(6
:End
:
:End

ConnectAI:
Code: [Select]
:For(A,1,7
:Matr►List[A],A,L1
:sum(L1≠0)+E→E
:End
:If E<5
:Goto AA
:0→E
:For(A,1,7
:0→B
:Repeat [A](Ans,A) or Ans=7
:Ans+1
:End
:Ans-1→C
:{C,A,1,0,P+1,1,0
:prgmCHECKBRD
:{C,A,‾1,1,Ans(5),1,0
:prgmCHECKBRD
:{C,A,0,1,Ans(5),1,0
:prgmCHECKBRD
:{C,A,1,1,Ans(5),1,0
:prgmCHECKBRD
:End
:real(E→E
:If E≠0
:Then
:If fPart(M)=.5
:not(P→P
:P+1→[A](10fPart(E),iPart(E
:iPart(M→M
:Else
:If fPart(M)=.5
:Return
:not(P→P
:M+.5→M
:prgmCONECTAI
:If fPart(M)=0
:Return
:M-.5→M
:not(P→P
:Lbl AA
:Repeat [A](1,Ans)=0
:randInt(1,7
:End
:Ans→B
:0
:Repeat Ans=7 or [A](Ans,B)
:Ans+1
:End
:P+1→[A](Ans-1,B
:Return
:End

Connect5:
Code: [Select]
:
:0
:Menu("MODE?","NO AI/NO CHECK",0,"NO AI/CHECK",1,"EASY AI/CHECK",2
:Lbl 2
:Ans+1
:Lbl 1
:Ans+1
:Lbl 0
:Ans→M
:DelVar [A]
:{7,7→dim([A]
:0→P
:prgmDRAWBOAR
:Repeat K=45
:Output(1,1,P
:getKey→K
:If K=24
:R-1→R
:If K=26
:R+1→R
:If R>7
:1→R
:If R<1
:7→R
:Output(2,1,"                "
:Output(2,2R,"V
:If K=21
:Then
:If not([A](1,R
:Then
:0
:Repeat [A](Ans,R) or Ans=7
:Ans+1
:End
:Ans-1→C
:P+1→[A](Ans,R
:prgmDRAWBOAR
:
:If M=1 or M=2
:Then
:0→E
:R→A
:{C,A,1,0,P+1,0,0
:prgmCHECKBRD
:{C,A,‾1,1,Ans(5),0,0
:prgmCHECKBRD
:{C,A,0,1,Ans(5),0,0
:prgmCHECKBRD
:{C,A,1,1,Ans(5),0,0
:prgmCHECKBRD
:If E>0
:Then
:Output(1,1,"YOU WIN!!!
:Return
:End
:End
:
:
:not(P→P
:
:If M=2:Then
:Output(1,1,"THINKING...
:prgmCONECTAI
:not(P→P
:prgmDRAWBOAR
:Output(1,1,"           "
:End
:
:End
:
:End
:End
:Return

DrawBoar:
Code: [Select]
:ClrHome
:Output(3,1,"1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1"
:Output(1,1,"DRAWING...
:For(B,1,7
:For(C,1,6
:Output(C+2,2B,sub(" 0O",[A](C,B)+1,1
:End
:End
:Output(1,1,"                "
:
« Last Edit: March 13, 2010, 06:20:01 pm by _player1537 »

Offline Radical Pi

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1143
  • Rating: +5/-2
    • View Profile
    • RealityRevolution
Re: A typical Connect 4 game in TI-Basic
« Reply #16 on: March 13, 2010, 07:01:10 pm »
Uh... If you wouldn't mind, could you explain what some of the variables are? And especially what the elements of the list that CheckBrd needs are?

I really am terrible at reading other people's code v_v
One of these days I'll get a sig I'm really proud of.

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: A typical Connect 4 game in TI-Basic
« Reply #17 on: March 13, 2010, 09:44:29 pm »
That looks cool. :D  I was wondering, why does it say "Connect 5"? Is it a typo? :)

It looks pretty good.  Great job _player! ;D

_player1537

  • Guest
Re: A typical Connect 4 game in TI-Basic
« Reply #18 on: March 13, 2010, 09:50:47 pm »
no problem, I'm gonna have to study it too, but I think I remember it...

Variable A is the column that we are checking, starting at one ending at seven.
Variable C uppermost spot that something can be placed, such as if you have a column with 4 holes filled, C will be the hole above the            .   highest blackened in spot
C and A are the first two list elements.
the 3rd element is the direction in the Y direction, such as if it is 1 it will be going Up first, 0 it won't go up at all, -1 it will go down first
the 4th element is the X direction
the 5th element is the piece it is looking for, such as if it is 1 it will be searching for every piece that is a 1
the 6th element is the number of pieces that are in a row, it starts at one because when it is checking for pieces there will always be one, piece, the one you place down
the 7th is... I really don't remember, it might have been something I threw in for testing

E the place where there is an instant win/lose
M is the Mode you are in.  I used it for one more thing which I'll try to explain.  It would first check for an instant win/lose for the AI, then if there was no instant win/lose it would check for the opponent too... actually it was probably something else because I don't see why I would do that... sorry I'll keep thinking for that
Matrix A is the matrix the board is stored in


I think that is it, sorry for the very messy post, but I hope that helped clarify for some people if not just tell me and I'll try to explain it better :)


Edit: its called connect5 because I made connect4 at first and then wanted to try something different without messing up all my code, so I copied it to a new one called connect 5     Thank you, I thought it was kinda redundant :P
« Last Edit: March 13, 2010, 09:52:47 pm by _player1537 »

Offline Radical Pi

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1143
  • Rating: +5/-2
    • View Profile
    • RealityRevolution
Re: A typical Connect 4 game in TI-Basic
« Reply #19 on: March 13, 2010, 10:37:00 pm »
Thanks, now I understand much more of it!
I think I'm going to update my Connect 4 program with a modified version of your method to check the board for wins.

Also, am I right in saying that your board is stored sideways? As in, the bottom row in my version is [A](7,x) but in yours it's [A](1,x)? I might be able to shave off a few more bytes by switching to this convention...

Yay for insight ;D
One of these days I'll get a sig I'm really proud of.

_player1537

  • Guest
Re: A typical Connect 4 game in TI-Basic
« Reply #20 on: March 13, 2010, 10:55:44 pm »
actually, I think it is stored where the 6th row from the top is the bottom row, the 7th is just incase

idk, Matrices have always confused me, at least the part where you take a specific chunk out of it ([a](3,5))

Offline Radical Pi

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1143
  • Rating: +5/-2
    • View Profile
    • RealityRevolution
Re: A typical Connect 4 game in TI-Basic
« Reply #21 on: March 13, 2010, 11:05:41 pm »
I just remember that matrices are (row,column) and that's enough for me.

And that explains why your board was 7x7... I was wondering about that as well.
One of these days I'll get a sig I'm really proud of.