Page 1 of 1

What from here?

Posted: Mon Mar 23, 2009 7:57 pm
by clc02
I just finished my tic-tac-toe game(cpp). (Not when clicked though, I figure easy to add with some sdl events(?))
I was thinking of add a computer to play against I'm definitely making it into arrays instead of the char row1collum1 = '_'; though. Will be easy to choose spots instead of checking two characters with a ton of if's to see where to put it. :evil:
After I finish that up any other ideas on small things I can create? I finished this in about 30 minutes.

Also I have a small question on SDL. Is their a way to change camera positioning? Like having two squares with only one fitting on the screen at the time and having the squares move. Or do I have to do it with moving all the sprites around then updating the map?
Meh. 151 lines of very inefficient coding.

Code: Select all

void ticTacToe(void)
{
     system("CLS");
     /* Board set up */
     char cPlayer = 'X';
     char rChoice;
     char cChoice;
     char r1c1 = '_';
     char r1c2 = '_';
     char r1c3 = '_';
     char r2c1 = '_';
     char r2c2 = '_';
     char r2c3 = '_';
     char r3c1 = '_';
     char r3c2 = '_';
     char r3c3 = '_';
     char Winner = '_';
     /* Find player spot */
     cout<<123<<endl;
     cout<<'1'<<r1c1<<r1c3<<r1c3<<endl<<'2'<<r2c1<<r2c2<<r2c3<<endl<<'3'<<r3c1<<r3c2<<r3c3<<endl;
     cout<<"Type the row then the collum player X."<<endl;
     for(int i=1;i!=10;i++)
     {
     cin >> rChoice;
     cin >> cChoice;
     /* Would of been alot easier with arrays >.< */
     if (rChoice=='1')
     {
                    if(cChoice=='1')
                    {
                                  if (r1c1 == '_')
                                  {
                                           r1c1 = cPlayer;
                                           }
                                  }
                    if(cChoice=='2')
                    {
                                  if (r1c2 == '_')
                                  {
                                           r1c2 = cPlayer;
                                           }
                                  }
                    if(cChoice=='3')
                    {
                                  if (r1c3 == '_')
                                  {
                                           r1c3 = cPlayer;
                                           }
                                  }
     }
     if (rChoice=='2')
     {
                      cout << rChoice<<endl;
                    if(cChoice=='1')
                    {
                                  if (r2c1 == '_')
                                  {
                                           r2c1 = cPlayer;
                                           }
                                  }
                    if(cChoice=='2')
                    {
                                  if (r2c2 == '_')
                                  {
                                           r2c2 = cPlayer;
                                           }
                                  }
                    if(cChoice=='3')
                    {
                                  if (r2c3 == '_')
                                  {
                                           r2c3 = cPlayer;
                                           }
                                  }
     }
     if (rChoice=='3')
     {
                    if(cChoice=='1')
                    {
                                  if (r3c1 == '_')
                                  {
                                           r3c1 = cPlayer;
                                           }
                                  }
                    if(cChoice=='2')
                    {
                                  if (r3c2 == '_')
                                  {
                                           r3c2 = cPlayer;
                                           }
                                  }
                    if(cChoice=='3')
                    {
                                  if (r3c3 == '_')
                                  {
                                           r3c3 = cPlayer;
                                           }
                                  }
     }
     /* Peform a win check.  Again easier with rows >.< */
     if(r1c1 == r1c2 && r1c2 == r1c3)
     {
             cout <<"Player:"<<r1c1<<" wins!c:1" <<endl;
             Winner = r1c1;
             }
     if(r2c1 == r2c2 && r2c2 == r2c3 && r2c1!='_')
     {
             cout <<"Player:"<<r2c1<<" wins!c:2" <<endl;
             Winner = r2c1;
             }
     if(r3c1 == r3c2 && r3c2 == r3c3 && r3c1!='_')
     {
             cout <<"Player:"<<r1c1<<" wins!c:3" <<endl;
             Winner = r3c1;
             }
     if(r1c1 == r2c2 && r2c2 == r3c3 && r1c1!='_')
     {
             cout <<"Player:"<<r1c1<<" wins!c:4" <<endl;
             Winner = r1c1;
             }
     if(r1c3 == r2c2 && r2c2 == r3c1 && r1c3!='_')
     {
             cout <<"Player:"<<r1c1<<" wins!c:5" <<endl;
             Winner = r1c3;
             }
     if(r1c1 == r2c1 && r2c1 == r3c1 && r1c1!='_')
     {
             cout <<"Player:"<<r1c1<<" wins!c:6" <<endl;
             Winner = r1c1;
             }
     //Swapping players
     if (cPlayer == 'X')
     {
                 cPlayer = 'O';
                 }
     else
     {
         cPlayer = 'X';
         }
     cout<<r1c1<<r1c2<<r1c3<<endl<<r2c1<<r2c2<<r2c3<<endl<<r3c1<<r3c2<<r3c3<<endl;
     if (Winner != '_')
     {break;}
     else{
     cout <<Winner<<endl;
     cout<<"Type the row then the collum player "<<cPlayer<<'.'<<endl;
     }
     }
     //Ain't I classy.  Any way to get rid of this?
     cout << "Performing a input dump.  Please type something and press enter.  No spaces or it will get hung up.";
     string StringDump;
     cin >> StringDump;
 }

Re: What from here?

Posted: Tue Mar 24, 2009 6:17 pm
by dandymcgee
clc02 wrote:I was thinking of add a computer to play against [...].
After I finish that up any other ideas on small things I can create? I finished this in about 30 minutes.
Well you've only completed the easiest part, adding AI to tic-tac-toe is probably the hardest thing I've ever attempted (depending on how perfect you want it). As far as other things I hear tetris recommended a lot, that ought to keep you busy for quite some time while forcing you to learn many important programming habits.

Re: What from here?

Posted: Tue Mar 24, 2009 6:19 pm
by eatcomics
Tetris will be a challenge, just a warning... People tell other to start with it but trust me it's a lot more difficult than you would expect....

Re: What from here?

Posted: Tue Mar 24, 2009 7:24 pm
by dandymcgee
eatcomics wrote:Tetris will be a challenge, just a warning... People tell other to start with it but trust me it's a lot more difficult than you would expect....
Same with tic-tac-toe AI :) But they're definitely excellent exercises if you really are determined.

Re: What from here?

Posted: Thu Mar 26, 2009 8:10 am
by MarauderIIC
You can brute-force tic-tac-toe AI. But that's no fun. Instead you should read up on A* and Alpha-beta pruning (AKA Alpha-beta search). Or minimax search, since your search area is "small"