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

0 Members and 1 Guest are viewing this topic.

Offline Radical Pi

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1143
  • Rating: +5/-2
    • View Profile
    • RealityRevolution
A typical Connect 4 game in TI-Basic
« on: March 09, 2010, 12:51:08 am »
EDIT 2: This code is outdated. The final version is in a later post in this topic.

This is what I've been working on all day. It isn't completely finished; I realized just a few minutes ago that I totally forgot to handle a tied game, and it doesn't give you any fancy victory message when you win. And you will win, since the AI opponent is just "choose a random column." But I didn't make this for the AI; I made it for everything else.

The main program is MAINLOOP (obviously) and I made GRAVITY a subprogram because it saves a few bytes. That brings me to the main reason I made this topic... I want to see if this can be optimized any more than it already is. I think I've already done a decent enough job on it, but I really want to make it as small as it can be.

I've attached both programs, and here's the code too:

Code: [Select]
MAINLOOP
:Disp "WILL YOU MOVE
:Input "FIRST? Y/N: ",Str1
:Str1≠"Y→T
:{6,7→dim([A]
:Fill 0,[A]
:ClrHome
:For(A,2,7
:Output(A,2,". . . . . . .
:End
:Output(8,2,"-------------
:Repeat W
:If T
:Then
:Repeat not([A](1,Ans
:randInt(1,7→C
:End
:prgmGRAVITY
:Else
:DelVar M4→C
:Repeat M
:Output(1,2C,"V
:Repeat max(K={21,24,26,45
:getKey→K
:End
:If K=45
:Return
:If K=21 and not([A](1,C
:Then
:prgmGRAVITY
:1→M
:Output(1,2C,"
:End
:If K≠21
:Then
:Output(1,2C,"
:C-25+Ans→C
:Ans-7(Ans=8)+7not(Ans→C
:End
:End
:End
:{1,1,1,1→{L1}
:T+1→T
:1→A
:While A<4 and R+A≤6 and Ans
:If T=[A](R+A,C
:Then
:1+{L1}(1→{L1}(1
:A+1→A
:Else
:0
:End
:End
:1→A
:While A<4 and C-A≥1 and Ans
:If T=[A](R,C-A
:Then
:1+{L1}(2→{L1}(2
:A+1→A
:Else
:0
:End
:End
:1→A
:While A<4 and C+A≤7 and Ans
:If T=[A](R,C+A
:Then
:1+{L1}(2→{L1}(2
:A+1→A
:Else
:0
:End
:End
:1→A
:While A<4 and R+A≤6 and C+A≤7 and Ans
:If T=[A](R+A,C+A
:Then
:1+{L1}(3→{L1}(3
:A+1→A
:Else
:0
:End
:End
:1→A
:While A<4 and R-A≥1 and C-A≥1 and Ans
:If T=[A](R-A,C-A
:Then
:1+{L1}(3→{L1}(3
:A+1→A
:Else
:0
:End
:End
:1→A
:While A<4 and R+A≤6 and C-A≥1 and Ans
:If T=[A](R+A,C-A
:Then
:1+{L1}(4→{L1}(4
:A+1→A
:Else
:0
:End
:End
:1→A
:While A<4 and R-A≥1 and C+A≤7 and Ans
:If T=[A](R-A,C+A
:Then
:1+{L1}(4→{L1}(4
:A+1→A
:Else
:0
:End
:End
:max(3<{L1}→W
:not(T-1→T
:End

GRAVITY
:DelVar  RR
:Repeat Ans=5 or [A](Ans+1,C
:Ans+1
:End
:Ans+(Ans=5 and not([A](6,C→R
:T+1→[A](Ans,C
:Output(R+1,2C,sub("XO",T+1,1

In particular, I'm wondering if there's a less ridiculous way of figuring out when a player wins (which is the section in MAINLOOP from the :{1,1,1,1→{L1} line to the :max(3<{L1}→W line). And I know I could already save a few bytes in that section by incrementing T for its duration. But what else? I've hit my limit already :(

EDIT: I did that T+1→T optimization and saved six bytes. And it also made me catch a bug I didn't even know was there. I've updated the attached file with this fix too.
« Last Edit: March 10, 2010, 05:08:20 pm by Nyrax »
One of these days I'll get a sig I'm really proud of.

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: A typical Connect 4 game in TI-Basic
« Reply #1 on: March 09, 2010, 12:59:20 am »
Hmmm...I don't have time to look over it a whole lot right now (since I'm probably gonna have to pull an all-nighter from homework already...) but I will check it out when I have time. It looks nice though.
By the way, by the time I get a chance to look at it, it will already have been looked at by someone more experienced :P
« Last Edit: March 09, 2010, 01:00:03 am by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: A typical Connect 4 game in TI-Basic
« Reply #2 on: March 09, 2010, 01:18:38 am »
Cool to hear you're starting coding ^^

Also good idea to start with smaller projects too (like Connect 4), some people usually start with a massive RPG or platformer x.x.

Unfortunately I am not as good for optimizing and reading people code, so it migth be better someone else check the code for deepest optimizations.

I'll give it a try later, though. :)

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: A typical Connect 4 game in TI-Basic
« Reply #3 on: March 09, 2010, 01:48:32 am »
Isn't Eeems pretty good at spotting optimizations?
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline Eeems

  • Mr. Dictator
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6265
  • Rating: +318/-36
  • little oof
    • View Profile
    • Eeems
Re: A typical Connect 4 game in TI-Basic
« Reply #4 on: March 09, 2010, 01:54:42 am »
I'm ok at some, but that requires me to have the time to sit and read through the code. And unfortunatly I just saw this right as I decided it was too late for me to be up, so I'll have to wait until tomorrow before diving into this.

Edit: hmm, I took a quick peek, and isn't there a way to add all the columns of a matrix? Or is it rows? That could help for checking for a win.
« Last Edit: March 09, 2010, 01:59:06 am by Eeems »
/e

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: A typical Connect 4 game in TI-Basic
« Reply #5 on: March 09, 2010, 01:57:08 am »
Ah ok, I just saw that you looked over tifreak's code so ya, just thought you might be. But ya, I know what you mean.
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline Eeems

  • Mr. Dictator
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6265
  • Rating: +318/-36
  • little oof
    • View Profile
    • Eeems
Re: A typical Connect 4 game in TI-Basic
« Reply #6 on: March 09, 2010, 02:00:59 am »
Well I'm pretty good at optimizing my own code, but it's not so easy for me to optimize other peoples code.
I do consider myself an ok optimizer though.
/e

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: A typical Connect 4 game in TI-Basic
« Reply #7 on: March 09, 2010, 02:02:34 am »
Ya, I would bet that is how most people are. Because you know what you're trying to do with your own code so you kinda see the things better, if that makes sense. But cool...I need to get better at it.
Back on topic though, as I said, I shall check this out when I have time :)
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

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 #8 on: March 09, 2010, 02:12:24 am »
There's cumSum( for finding the cumulative sum of a matrix's columns, but that can't really help me at all, not the way I'm currently storing the grid. Plus, it would only do anything useful for the downwards victory.
The fact that you can win diagonally is what makes this so hard to make more efficient, I think.
One of these days I'll get a sig I'm really proud of.

Offline Eeems

  • Mr. Dictator
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6265
  • Rating: +318/-36
  • little oof
    • View Profile
    • Eeems
Re: A typical Connect 4 game in TI-Basic
« Reply #9 on: March 09, 2010, 09:14:23 am »
Well cumSum() can also help if you T[A] which just rotates the matrix.
But yeah diagnal is kind of hard.

EDIT (DJ_Omni made the "T" an exponent with bbcode tags since it could have been confusing)
« Last Edit: March 09, 2010, 03:51:33 pm by DJ Omnimaga »
/e

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 #10 on: March 09, 2010, 10:20:05 am »
That looks very nice!  I think the check code is as optimized as it can get in its current state.  You could probably do something tricky to get it smaller, but I'm not sure what you would have to do.  It looks great!  Great job! ;D

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 #11 on: March 10, 2010, 05:15:07 pm »
Here are the finished versions of both programs. Now it doesn't go into an infinite loop if you fill up the board :D
Again, if anyone sees any optimizations, don't hesitate to tell me; I literally want to shave off every byte possible. I still think there must be a better way to check if a player has won, but I have no idea how.

Also, if anyone has any idea how I could improve the AI (as in, make an AI that does more than choose a random unfilled column), I'd love to hear it.

Anyway, here's the code. A zip of it is attached too.

Code: (prgmCONNECT4) [Select]
:Disp  "WILL YOU MOVE
:Input "FIRST? Y/N: ",Str1
:Str1≠"Y→T
:{6,7→dim([A]
:Fill 0,[A]
:ClrHome
:For(A,2,7
:Output(A,2,". . . . . . .
:End
:Output(8,2,"-------------
:DelVar URepeat W or U=42
:If T
:Then
:Repeat not([A](1,Ans
:randInt(1,7→C
:End
:prgmZG
:Else
:DelVar M4→C
:Repeat M
:Output(1,2C,"V
:Repeat max(K={21,24,26,45
:getKey→K
:End
:If K=45
:Return
:If K=21 and not([A](1,C
:Then
:prgmZG
:1→M
:Output(1,2C,"
:End
:If K≠21
:Then
:Output(1,2C,"
:C-25+Ans→C
:Ans-7(Ans=8)+7not(Ans→C
:End
:End
:End
:{1,1,1,1→L1
:T+1→T
:1→A
:While A<4 and R+A≤6 and Ans
:If T=[A](R+A,C
:Then
:1+L1(1→L1(1
:A+1→A
:Else
:0
:End
:End
:1→A
:While A<4 and C-A≥1 and Ans
:If T=[A](R,C-A
:Then
:1+L1(2→L1(2
:A+1→A
:Else
:0
:End
:End
:1→A
:While A<4 and C+A≤7 and Ans
:If T=[A](R,C+A
:Then
:1+L1(2→L1(2
:A+1→A
:Else
:0
:End
:End
:1→A
:While A<4 and R+A≤6 and C+A≤7 and Ans
:If T=[A](R+A,C+A
:Then
:1+L1(3→L1(3
:A+1→A
:Else
:0
:End
:End
:1→A
:While A<4 and R-A≥1 and C-A≥1 and Ans
:If T=[A](R-A,C-A
:Then
:1+L1(3→L1(3
:A+1→A
:Else
:0
:End
:End
:1→A
:While A<4 and R+A≤6 and C-A≥1 and Ans
:If T=[A](R+A,C-A
:Then
:1+L1(4→L1(4
:A+1→A
:Else
:0
:End
:End
:1→A
:While A<4 and R-A≥1 and C+A≤7 and Ans
:If T=[A](R-A,C+A
:Then
:1+L1(4→L1(4
:A+1→A
:Else
:0
:End
:End
:max(3<L1→W
:not(T-1→T
:U+1→U
:End
:ClrHome
:If Ans≠42 and W
:Then
:Disp "YOU "+sub("LOSEWIN!",4T+1,4
:Else
:"TIED GAME

Code: (prgmZG) [Select]
:DelVar  RR
:Repeat Ans=5 or [A](Ans+1,C
:Ans+1
:End
:Ans+(Ans=5 and not([A](6,C→R
:T+1→[A](Ans,C
:Output(R+1,2C,sub("XO",T+1,1
« Last Edit: March 10, 2010, 05:50:33 pm by Nyrax »
One of these days I'll get a sig I'm really proud of.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: A typical Connect 4 game in TI-Basic
« Reply #12 on: March 10, 2010, 11:19:14 pm »
cool I'll update the file on my computer. I didn't got time to try the other version because of work and I was busy messing with my Nspire and Axe. Hopefully I should be able to test later (and maybe do a screenshot)

_player1537

  • Guest
Re: A typical Connect 4 game in TI-Basic
« Reply #13 on: March 13, 2010, 02:20:38 pm »
I was working on a project like this a couple weeks ago.  I also tried to make an AI if it would help you.  atm it only checks for an instant win/lose then places it, otherwise it puts a random peice.  If you'd like to look at it I'll send it to the computer.  Hope I can help.

(Also I forgot to mention that my code can be far from readable as I abuse the Ans variable :P)

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 #14 on: March 13, 2010, 04:03:38 pm »
I'd love to take a look at your AI, especially if it can help me optimize my win-checking code, which it sounds like it can
One of these days I'll get a sig I'm really proud of.