What from here?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
clc02
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Sun Mar 22, 2009 9:55 pm

What from here?

Post 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;
 }
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: What from here?

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: What from here?

Post 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....
Image
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: What from here?

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: What from here?

Post 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"
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply