Archived

This topic is now archived and is closed to further replies.

DarkWhoppy

Tic-Tac-Toe Game...

Recommended Posts

I am trying to figure out how to use a 2D array. Just doing this little Tic-Tac-Toe game to help for me to see how they work in action. But im kinda stuck...Can someone point me in the right direction? (i dont wanna start a project and now finish it ) Half of the code I've been changing around...but I can't seem to get my 2D array to work. -----Code Removed by ME!------- [edited by - DarkWhoppy on July 27, 2003 6:21:55 PM]

Share this post


Link to post
Share on other sites
http://www.cplusplus.com/doc/tutorial/tut3-1.html

Scroll down to multidimentional arrays. Or, examine this:


char board[3][3]; // a 3 x 3 array

if ( (''X'' == board[0][0] && ''X'' == board[1][1] && ''X'' == board[2][2])
|| (''X'' == board[0][0] && ''X'' == board[0][1] && ''X'' == board[0][2])
|| (all the other cases for an X win)
) {

// X won, so do stuff
}

2D arrays aren''t terribly complicated.

Also, the result of a winning condition for either player is always the same, so you could shorten/simplify your code a lot by lumping all the winning cases for a given player together with OR.

Share this post


Link to post
Share on other sites
...that doesn't help much. But I rewrote the code. Trying a different way now... I can't get the AI to work....

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

---Code Removed by ME!!---
[edited by - DarkWhoppy on July 27, 2003 6:20:05 PM]


[edited by - DarkWhoppy on July 27, 2003 6:21:07 PM]

[edited by - DarkWhoppy on July 28, 2003 6:03:41 PM]

Share this post


Link to post
Share on other sites
Why should he? If it's a 2D game board, model it as a 2D array...
Makes sense to me.

Also, seriously consider

if ('0' == Board[0][0]) { ... }

instead of

if (Board[0][0] == '0') { ... }

so that if you miss an = , you get a compile-time error instead of a run-time error or unexpected results.

Edit: He'd at least want to use board[9] over board[8], now that I think of it...

[edited by - Geoff the Medio on July 27, 2003 12:56:13 AM]

Share this post


Link to post
Share on other sites
I don''t know if this is a dumb question but......if you use if statements for every possible move in tic tac toe, thats not considered AI right? because it will do the same move over and over.

Share this post


Link to post
Share on other sites
quote:
Original post by pimple
true...
btw, your code looks so messy... take a look at this:
http://www.geocities.com/antigatez/pimplesttt.zip
only 412 lines for ya!


That seems like a lot, I''m going to see if I can''t do better. Post back later.

quote:
Original post by TheOne1
I don''t know if this is a dumb question but......if you use if statements for every possible move in tic tac toe, thats not considered AI right? because it will do the same move over and over.


You can call pretty much whatever you want AI. It doesn''t matter how it comes up with the solutions.

Share this post


Link to post
Share on other sites
I was thinking of using

num = rand() 6 + 3;

and some IF''s to chose the computers places for the ''O''. And if its taken then redo the entire function until it finds an open place....then when thats done call another function to check the board to see if anyone has 3 X''s or O''s in a row :D

Share this post


Link to post
Share on other sites
quote:

That seems like a lot, I''m going to see if I can''t do better. Post back later.



well, it''s more about QUALITY... if you actually play my game, you''ll see how effective those 412 lines really are XD

anyhoo, keep up the good work!

Share this post


Link to post
Share on other sites
quote:
Original post by pimple
true...
btw, your code looks so messy... take a look at this:
http://www.geocities.com/antigatez/pimplesttt.zip
only 412 lines for ya!


BTW, your programs don''t work. What do you want the user to do? Type their name in? Type in either ''x'' or ''o''? I tried both of these scenarios, and the program just goes into an infinite loop, saying my input was invalid. So, before you go talking about how many lines your program has and telling someone else that their code is messy, make sure your own code WORKS!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by tremere
quote:
Original post by pimple
true...
btw, your code looks so messy... take a look at this:
http://www.geocities.com/antigatez/pimplesttt.zip
only 412 lines for ya!

BTW, your programs don''t work. What do you want the user to do? Type their name in? Type in either ''x'' or ''o''? I tried both of these scenarios, and the program just goes into an infinite loop, saying my input was invalid. So, before you go talking about how many lines your program has and telling someone else that their code is messy, make sure your own code WORKS!


Actually you are apparently supposed to type a number from one to nine, (a good ole user prompt would really come in handy pimple, trying to figure out what youre supposed to do is a pain) the numbering for the board is confusing as it goes from bottom left to top right, its also buggy, if a spot gets selected twice it says invalid move for the rest of the game whether the move is invalid or not, also if nobody wins then it says that the last user to play has won.

If it''s more about QUALITY pimple, then how come your code is buggy, and now that i played the game, i saw just how much more effective those 412 lines really should be, if you had thought it through a little more.
sorry to be so blunt.

Share this post


Link to post
Share on other sites
Hi. The man that said 1D arays were better was kinda right. Its a bit harder at the beginnin'' but you''ll have less things to write and its also an optimiztion since the offsets are easier to write. Plus, you can more easily write loops to parse 1D-arrays. My own TTT game use such loops.
Just give the sqrs numbers like the following:
0 1 2
3 4 5
6 7 8.
PS.Though, if you''re not too fluent with prog and maths you might all the same prefer using a 2D array...

Prog, Hex & Rock''n''Roll :
I don''t like the Prog but the Prog likes me.
Some nice poetry to sweeten your spirit and relax a bit before programming

Share this post


Link to post
Share on other sites
isnt it easier to just use numeric keypad for entering moves? It looks exactly like tic-tac-toe board...

And AI is simple: check if there''s a move that wins, else check if there''s opponents move that wins and block it, else put it anywhere...

Share this post


Link to post
Share on other sites
DarkWhoppy,

I''m not sure if this will help or not, but this is the way I determined my computers move.

FIRST: Check to see if the computer can win anywhere in one move. If so, move there.

SECOND: Check the board and see if the player can win in one move ANYWHERE. If the player can, then Block that square.

THIRD: Check to see if the computer can make 2-in-a-row anywhere, and have an open square in the third position. This will set him up for a win next round, unless the player blocks it.

FOURTH: Try to move to the middle square. If it''s blocked, then continue.

FIFTH: Try to move to a random corner square. If they are all blocked, then continue.

SIXTH: If all else fails, Move to a random side square.


~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites
My bet my Tic-Tac-Toe AI could beat up your Tic-Tac-Toe AI.

Deleted my other posts and moved the code here. If anyone wants to share their code so we can have them fight then post it.

Not sure what I have to bet with, though.

[edited by - smart_idiot on July 30, 2003 3:57:49 PM]

Share this post


Link to post
Share on other sites