Jump to content
  • Advertisement
Sign in to follow this  
Catkill

Tic Tac Toe

This topic is 3595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So ive been working on Tic Tac Toe but it doesnt work correctly , can anyone see the errors (not compiler errors just errors in the actual design) especialy not picking up on the Win and sometimes with the ai. Not laid out very well either I know i shouldnt use global variables but cant help myself heres the code: // Tic Tac Toe.cpp #include "stdafx.h" #include <iostream> #include <time.h> //Prototypes void Draw_Board(void); void Init(void); int Win(void); bool Used[9]; char Player = 'x'; char Computer = 'o'; char Board[3][3]; bool GameEnd; int Ai_Rand; bool Player_Turn; bool Computer_Turn; void Init(void) { Board[0][0] = '1'; Board[0][1] = '2'; Board[0][2] = '3'; Board[1][0] = '4'; Board[1][1] = '5'; Board[1][2] = '6'; Board[2][0] = '7'; Board[2][1] = '8'; Board[2][2] = '9'; GameEnd = false; Player_Turn = true; Computer_Turn = false; } void Draw_Board(void) { std::cout << Board[0][0] << " | " << Board[0][1] << " | " << Board[0][2] << std::endl; std::cout << Board[1][0] << " | " << Board[1][1] << " | " << Board[1][2] << std::endl; std::cout << Board[2][0] << " | " << Board[2][1] << " | " << Board[2][2] << std::endl; } int Win(void) { if (((Board[0][0] && Board[0][1] && Board [0][2]) == Player) || ((Board[1][0] && Board[1][1] && Board [1][2]) == Player) || ((Board[2][0] && Board[2][1] && Board [2][2]) == Player) || ((Board[0][0] && Board[1][0] && Board [2][0]) == Player) || ((Board[0][1] && Board[1][1] && Board [2][1]) == Player) || ((Board[0][2] && Board[1][2] && Board [2][2]) == Player) || ((Board[0][0] && Board[1][1] && Board [2][2]) == Player) || ((Board[2][0] && Board[1][1] && Board [0][2]) == Player)) { GameEnd = true; return 1; } if (((Board[0][0] && Board[0][1] && Board [0][2]) == (Computer)) || ((Board[1][0] && Board[1][1] && Board [1][2])== (Computer)) || ((Board[2][0] && Board[2][1] && Board [2][2])== (Computer)) || ((Board[0][0] && Board[1][0] && Board [2][0])== (Computer)) || ((Board[0][1] && Board[1][1] && Board [2][1])== (Computer)) || ((Board[0][2] && Board[1][2] && Board [2][2])== (Computer)) || ((Board[0][0] && Board[1][1] && Board [2][2])== (Computer)) || ((Board[2][0] && Board[1][1] && Board [0][2])== (Computer))) { GameEnd = true; return 2; } else { return 0; } } int _tmain(int argc, _TCHAR* argv[]) { //initiate--------------------------------------------------- Init(); //Get input ------------------------------------------------- int Choice; std::cout << "Welcome to Tic Tac Toe" << std::endl; //Seed time ------------------------------------------------- srand(time(NULL)); //Draw starting Board--------------------------------------- Draw_Board(); //Enter game loop------------------------------------------ while (GameEnd == false) { while (Player_Turn == true) { //Get user input-------------------------------------- std::cout << "Please select a place for your move:\t"; std::cin >> Choice; //Change block---------------------------------------- switch (Choice) { case 1: { if (Used[0] == false) { Board[0][0] = Player; Used[0] = true; Player_Turn = false; } else { std::cout << "That square is already occupied" << std::endl; Player_Turn = true; } break; } case 2: { if (Used[1] == false) { Board[0][1] = Player; Used[1] = true; Player_Turn = false; } else { std::cout << "That square is already occupied" << std::endl; Player_Turn = true; } break; } case 3: { if(Used[2] == false) { Board[0][2] = Player; Used[2] = true; Player_Turn = false; } else { std::cout << "That square is already occupied" << std::endl; Player_Turn = true; } break; } case 4: { if(Used[3] == false) { Board[1][0] = Player; Used[3] = true; Player_Turn = false; } else { std::cout << "That square is already occupied" << std::endl; Player_Turn = true; } break; } case 5: { if(Used[4] == false) { Board[1][1] = Player; Used[4] = true; Player_Turn = false; } else { std::cout << "That square is already occupied" << std::endl; Player_Turn = true; } break; } case 6: { if(Used[5] == false) { Board[1][2] = Player; Used[5] = true; Player_Turn = false; } else { std::cout << "That square is already occupied" << std::endl; Player_Turn = true; } break; } case 7: { if(Used[6] == false) { Board[2][0] = Player; Used[6] = true; Player_Turn = false; } else { std::cout << "That square is already occupied" << std::endl; Player_Turn = true; } break; } case 8: { if(Used[7] == false) { Board[2][1] = Player; Used[7] = true; Player_Turn = false; } else { std::cout << "That square is already occupied" << std::endl; Player_Turn = true; } break; } case 9: { if(Used[7] == false) { Board[2][2] = Player; Used[4] = true; Player_Turn = false; } else { std::cout << "That square is already occupied" << std::endl; Player_Turn = true; } break; } default: { std::cout << "choice invalid , please re-enter" <<std::endl; Player_Turn = true; } } } //Redraw Board ---------------------------------------------------------- Draw_Board(); //Check for win --------------------------------------------------------- Win(); std::cout << "\n"; std::cout << "Opponents turn....\n"; Computer_Turn = true; Ai_Rand = rand()%9; while (Computer_Turn == true) { switch (Ai_Rand) { case 1: { if (Used[Ai_Rand] == false) { Board[0][0] = Computer; Used[0] = true; Computer_Turn = false; } else { Ai_Rand = rand()%9; Computer_Turn = true; } break; } case 2: { if (Used[Ai_Rand] == false) { Board[0][1] = Computer; Used[1] = true; Computer_Turn = false; } else { Ai_Rand = rand()%9; Computer_Turn = true; } break; } case 3: { if(Used[Ai_Rand] == false) { Board[0][2] = Computer; Used[2] = true; Computer_Turn = false; } else { Ai_Rand = rand()%9; Computer_Turn = true; } break; } case 4: { if(Used[Ai_Rand] == false) { Board[1][0] = Computer; Used[3] = true; Computer_Turn = false; } else { Ai_Rand = rand()%9; Computer_Turn = true; } break; } case 5: { if(Used[Ai_Rand] == false) { Board[1][1] = Computer; Used[4] = true; Computer_Turn = false; } else { Ai_Rand = rand()%9; Computer_Turn = true; } break; } case 6: { if(Used[Ai_Rand] == false) { Board[1][2] = Computer; Used[5] = true; Computer_Turn = false; } else { Ai_Rand = rand()%9; Computer_Turn = true; } break; } case 7: { if(Used[Ai_Rand] == false) { Board[2][0] = Computer; Used[6] = true; Computer_Turn = false; } else { Ai_Rand = rand()%9; Computer_Turn = true; } break; } case 8: { if(Used[Ai_Rand] == false) { Board[2][1] = Computer; Used[4] = true; Computer_Turn = false; } else { Ai_Rand = rand()%9; Computer_Turn = true; } break; } case 9: { if(Used[Ai_Rand] == false) { Board[2][2] = Computer; Used[4] = true; Computer_Turn = false; } else { Ai_Rand = rand()%9; Computer_Turn = true; } break; } } }Draw_Board(); Win(); std::cout << "Players turn" << std::endl; Player_Turn = true; } if (Win() == 1) { std::cout << "Congratulations you have won!!!!" << std::endl; } if (Win() == 2) { std::cout << "Unfortunatly you lost :(" << std::endl; } std::cout << "Press the enter key to exit"; std::cin.ignore(std::cin.rdbuf()->in_avail() + 1); return 0; } cheers Catkill

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure what you meant by your first sentence (do you want critique on your program design or not?), so I'll just give you hints about the two logical errors you asked about.

Look at one condition in your Win function:
((Board[0][0] && Board[0][1] && Board [0][2]) == Player)
Hint: what do you think this expression:
(Board[0][0] && Board[0][1] && Board [0][2])
will evaluate to? Can it ever equal the character 'x' or the character 'o'?


As for the AI, you are currently trying to choose a random number 1 through 9 with this code:
Ai_Rand = rand()%9;
Hint: what is the value stored in Ai_Rand when the rand() function returns a 9?


P.S. Use source tags when you want to post a bunch of code like that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!