Vexer
June 20, 2009, 9:41am
1
So, Today I finished writing my 2 player Tic-Tac-Toe game. It is 250 lines of code completely written by me. I thought I would post the code here so that anyone learning c++ like me could learn from my code. Any advice, constructive criticism, or comments are welcome!
#include <iostream>
using namespace std;
void DisplayBoard(char BoardArray]);
int CheckGameOver(char BoardArray]);
void ResetBoard(char* BoardArray]);
int main()
{
int Player1Move = 0;
int Player2Move = 0;
char BoardArray[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
char BoardNumbers[9] = {'0','1','2','3','4','5','6','7','8'};
system("color 1f");
while(1)
{
system("CLS");
cout << "Tic-Tac-Toe\nBy: Spencer Davis\n";
cout << "Press the number of the section you want to move to\n";
DisplayBoard(BoardNumbers);
DisplayBoard(BoardArray);
cout << "Player 1 (X) please move:";
cin >> Player1Move;
if((Player1Move >= 0 && Player1Move <=8) && BoardArray[Player1Move] == ' ')
{
BoardArray[Player1Move] = 'X';
}
else
{
cout << "Error in move...Press enter\n";
system("PAUSE > NULL");
continue;
}
system("CLS");
cout << "Tic-Tac-Toe\nBy: Spencer Davis\n";
cout << "Press the number of the section you want to move to\n";
DisplayBoard(BoardNumbers);
DisplayBoard(BoardArray);
switch(CheckGameOver(BoardArray))
{
case 0:
break;
case 1:
cout << "TIE! Press enter to play again!";
system("PAUSE > NULL");
for(int n = 0; n <= 8; n++)
{
BoardArray[n] = 'E';
}
continue;
case 2:
cout << "Player 1 wins! Press enter to play again!";
system("PAUSE > NULL");
for(int n = 0; n <= 8; n++)
{
BoardArray[n] = 'E';
}
continue;
case 3:
cout << "Player 2 wins! Press enter to play again!";
system("PAUSE > NULL");
for(int n = 0; n <= 8; n++)
{
BoardArray[n] = 'E';
}
continue;
}
cout << "Player 2 (O) please move:";
cin >> Player2Move;
if((Player2Move >= 0 && Player2Move <=8) && BoardArray[Player2Move] == ' ')
{
BoardArray[Player2Move] = 'O';
}
else
{
cout << "Error in move...Press enter\n";
system("PAUSE > NULL");
continue;
}
DisplayBoard(BoardArray);
switch(CheckGameOver(BoardArray))
{
case 0:
break;
case 1:
cout << "TIE! Press enter to play again!";
system("PAUSE > NULL");
for(int n = 0; n <= 8; n++)
{
BoardArray[n] = 'E';
}
continue;
case 2:
cout << "Player 1 wins! Press enter to play again!";
system("PAUSE > NULL");
for(int n = 0; n <= 8; n++)
{
BoardArray[n] = 'E';
}
continue;
case 3:
cout << "Player 2 wins! Press enter to play again!";
system("PAUSE > NULL");
for(int n = 0; n <= 8; n++)
{
BoardArray[n] = 'E';
}
continue;
}
}
return 0;
}
void DisplayBoard(char BoardArray])
{
cout << " | | \n";
cout << " " << BoardArray[0] << " | " << BoardArray[1] << " | " << BoardArray[2] << "\n";
cout << "____|____|____\n";
cout << " | | \n";
cout << " " << BoardArray[3] << " | " << BoardArray[4] << " | " << BoardArray[5] << "\n";
cout << "____|____|____\n";
cout << " | | \n";
cout << " " << BoardArray[6] << " | " << BoardArray[7] << " | " << BoardArray[8] << "\n";
cout << " | | \n\n";
}
int CheckGameOver(char BoardArray])
{
bool Tie = false;
bool Player1Win = false;
bool Player2Win = false;
int ErrorCode = 0;
for(int n = 0; n <= 8; n++)
{
if(BoardArray[n] == ' ')
{
Tie = false;
break;
}
else
{
Tie = true;
}
}
if(BoardArray[0] == 'X' && BoardArray[3] == 'X' && BoardArray[6] == 'X')
{
Player1Win = true;
}
if(BoardArray[0] == 'X' && BoardArray[1] == 'X' && BoardArray[2] == 'X')
{
Player1Win = true;
}
if(BoardArray[2] == 'X' && BoardArray[5] == 'X' && BoardArray[8] == 'X')
{
Player1Win = true;
}
if(BoardArray[6] == 'X' && BoardArray[7] == 'X' && BoardArray[8] == 'X')
{
Player1Win = true;
}
if(BoardArray[0] == 'X' && BoardArray[4] == 'X' && BoardArray[8] == 'X')
{
Player1Win = true;
}
if(BoardArray[2] == 'X' && BoardArray[4] == 'X' && BoardArray[6] == 'X')
{
Player1Win = true;
}
if(BoardArray[1] == 'X' && BoardArray[4] == 'X' && BoardArray[7] == 'X')
{
Player1Win = true;
}
if(BoardArray[3] == 'X' && BoardArray[4] == 'X' && BoardArray[5] == 'X')
{
Player1Win = true;
}
if(BoardArray[0] == 'O' && BoardArray[3] == 'O' && BoardArray[6] == 'O')
{
Player2Win = true;
}
if(BoardArray[0] == 'O' && BoardArray[1] == 'O' && BoardArray[2] == 'O')
{
Player2Win = true;
}
if(BoardArray[2] == 'O' && BoardArray[5] == 'O' && BoardArray[8] == 'O')
{
Player2Win = true;
}
if(BoardArray[6] == 'O' && BoardArray[7] == 'O' && BoardArray[8] == 'O')
{
Player2Win = true;
}
if(BoardArray[0] == 'O' && BoardArray[4] == 'O' && BoardArray[8] == 'O')
{
Player2Win = true;
}
if(BoardArray[2] == 'O' && BoardArray[4] == 'O' && BoardArray[6] == 'O')
{
Player2Win = true;
}
if(BoardArray[1] == 'O' && BoardArray[4] == 'O' && BoardArray[7] == 'O')
{
Player2Win = true;
}
if(BoardArray[3] == 'O' && BoardArray[4] == 'O' && BoardArray[5] == 'O')
{
Player2Win = true;
}
if(Tie == true)
{
ErrorCode = 1;
}
else
{
if(Player1Win == true)
{
ErrorCode = 2;
}
else
{
if(Player2Win == true)
{
ErrorCode = 3;
}
else
{
ErrorCode = 0;
}
}
}
return ErrorCode;
}
Vexer
June 23, 2009, 9:39pm
2
Anything? It’s been a while…
MarkO
June 24, 2009, 12:24am
3
Which compiler did you use??
MarkO
June 24, 2009, 10:27am
5
Vexer:
Visual C++ 2008
Good!! I just got that installed, along with VB and C# (for GGCO’s Serial Port Program )
I am working on getting to GGCO’s program… I can not locate the Source Code, just all of the system code :
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.3074
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
Vexer
June 24, 2009, 11:56am
6
Great! Please tell me what you think when you compile it.
Vexer
June 27, 2009, 8:50pm
7
Here is an updated version of the code with less bugs:
#include <iostream>
using namespace std;
void DisplayBoard(char BoardArray]);
int CheckGameOver(char BoardArray]);
void ResetBoard(char* BoardArray]);
int main()
{
int Player1Move = 0;
int Player2Move = 0;
char BoardArray[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
char BoardNumbers[9] = {'0','1','2','3','4','5','6','7','8'};
system("color 1f");
while(1)
{
system("CLS");
cout << "Tic-Tac-Toe\nBy: Spencer Davis\n";
cout << "Press the number of the section you want to move to\n";
DisplayBoard(BoardNumbers);
DisplayBoard(BoardArray);
cout << "Player 1 (X) please move:";
cin >> Player1Move;
if((Player1Move >= 0 && Player1Move <=8) && BoardArray[Player1Move] == ' ')
{
BoardArray[Player1Move] = 'X';
}
else
{
cout << "Error in move...Press enter\n";
system("PAUSE > NULL");
continue;
}
system("CLS");
cout << "Tic-Tac-Toe\nBy: Spencer Davis\n";
cout << "Press the number of the section you want to move to\n";
DisplayBoard(BoardNumbers);
DisplayBoard(BoardArray);
switch(CheckGameOver(BoardArray))
{
case 0:
break;
case 1:
cout << "TIE! Press enter to play again!";
system("PAUSE > NULL");
for(int n = 0; n <= 8; n++)
{
BoardArray[n] = 'E';
}
continue;
case 2:
cout << "Player 1 wins! Press enter to play again!";
system("PAUSE > NULL");
for(int n = 0; n <= 8; n++)
{
BoardArray[n] = 'E';
}
continue;
case 3:
cout << "Player 2 wins! Press enter to play again!";
system("PAUSE > NULL");
for(int n = 0; n <= 8; n++)
{
BoardArray[n] = 'E';
}
continue;
}
cout << "Player 2 (O) please move:";
cin >> Player2Move;
if((Player2Move >= 0 && Player2Move <=8) && BoardArray[Player2Move] == ' ')
{
BoardArray[Player2Move] = 'O';
}
else
{
cout << "Error in move...Press enter\n";
system("PAUSE > NULL");
continue;
}
DisplayBoard(BoardArray);
switch(CheckGameOver(BoardArray))
{
case 0:
break;
case 1:
cout << "TIE! Press enter to play again!";
system("PAUSE > NULL");
for(int n = 0; n <= 8; n++)
{
BoardArray[n] = 'E';
}
continue;
case 2:
cout << "Player 1 wins! Press enter to play again!";
system("PAUSE > NULL");
for(int n = 0; n <= 8; n++)
{
BoardArray[n] = 'E';
}
continue;
case 3:
cout << "Player 2 wins! Press enter to play again!";
system("PAUSE > NULL");
for(int n = 0; n <= 8; n++)
{
BoardArray[n] = 'E';
}
continue;
}
}
return 0;
}
void DisplayBoard(char BoardArray])
{
cout << " | | \n";
cout << " " << BoardArray[0] << " | " << BoardArray[1] << " | " << BoardArray[2] << "\n";
cout << "____|____|____\n";
cout << " | | \n";
cout << " " << BoardArray[3] << " | " << BoardArray[4] << " | " << BoardArray[5] << "\n";
cout << "____|____|____\n";
cout << " | | \n";
cout << " " << BoardArray[6] << " | " << BoardArray[7] << " | " << BoardArray[8] << "\n";
cout << " | | \n\n";
}
int CheckGameOver(char BoardArray])
{
bool Tie = false;
bool Player1Win = false;
bool Player2Win = false;
int ErrorCode = 0;
if(BoardArray[0] == 'X' && BoardArray[3] == 'X' && BoardArray[6] == 'X')
{
Player1Win = true;
}
if(BoardArray[0] == 'X' && BoardArray[1] == 'X' && BoardArray[2] == 'X')
{
Player1Win = true;
}
if(BoardArray[2] == 'X' && BoardArray[5] == 'X' && BoardArray[8] == 'X')
{
Player1Win = true;
}
if(BoardArray[6] == 'X' && BoardArray[7] == 'X' && BoardArray[8] == 'X')
{
Player1Win = true;
}
if(BoardArray[0] == 'X' && BoardArray[4] == 'X' && BoardArray[8] == 'X')
{
Player1Win = true;
}
if(BoardArray[2] == 'X' && BoardArray[4] == 'X' && BoardArray[6] == 'X')
{
Player1Win = true;
}
if(BoardArray[1] == 'X' && BoardArray[4] == 'X' && BoardArray[7] == 'X')
{
Player1Win = true;
}
if(BoardArray[3] == 'X' && BoardArray[4] == 'X' && BoardArray[5] == 'X')
{
Player1Win = true;
}
if(BoardArray[0] == 'O' && BoardArray[3] == 'O' && BoardArray[6] == 'O')
{
Player2Win = true;
}
if(BoardArray[0] == 'O' && BoardArray[1] == 'O' && BoardArray[2] == 'O')
{
Player2Win = true;
}
if(BoardArray[2] == 'O' && BoardArray[5] == 'O' && BoardArray[8] == 'O')
{
Player2Win = true;
}
if(BoardArray[6] == 'O' && BoardArray[7] == 'O' && BoardArray[8] == 'O')
{
Player2Win = true;
}
if(BoardArray[0] == 'O' && BoardArray[4] == 'O' && BoardArray[8] == 'O')
{
Player2Win = true;
}
if(BoardArray[2] == 'O' && BoardArray[4] == 'O' && BoardArray[6] == 'O')
{
Player2Win = true;
}
if(BoardArray[1] == 'O' && BoardArray[4] == 'O' && BoardArray[7] == 'O')
{
Player2Win = true;
}
if(BoardArray[3] == 'O' && BoardArray[4] == 'O' && BoardArray[5] == 'O')
{
Player2Win = true;
}
if(Player2Win == true)
{
ErrorCode = 3;
}
else
{
if(Player1Win == true)
{
ErrorCode = 2;
}
else
{
if(Tie == true)
{
ErrorCode = 3;
}
else
{
ErrorCode = 0;
}
}
}
return ErrorCode;
}
MarkO
June 28, 2009, 5:01am
8
I am curious? I got it to compile and run in Visual Studio 2008 C++, but I had to “paste” you code into the template that VS2008 setup…
// Tic_Tac_Toe_002.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv])
{
return 0;
}
// Tic_Tac_Toe_002.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void DisplayBoard(char BoardArray]);
int CheckGameOver(char BoardArray]);
void ResetBoard(char* BoardArray]);
int main()
{
int Player1Move = 0;
int Player2Move = 0;
char BoardArray[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
char BoardNumbers[9] = {'0','1','2','3','4','5','6','7','8'};
system("color 1f");
while(1)
{
<< BIG SNIP >>
}
return ErrorCode;
}
int _tmain(int argc, _TCHAR* argv])
{
main();
return 0;
}
Vexer
June 28, 2009, 12:08pm
9
Whenever you create the project select “win32 console application” then press next and check “Empty Project”, then press finish. After that, look in the solution explorer, right click the folder that says “Source Files” and click add then click “New Item”. Name it “Main.cpp”. Now just copy and paste my code in that blank page.
MarkO
June 29, 2009, 12:04am
10
Vexer:
Whenever you create the project select “win32 console application” then press next and check “Empty Project”, then press finish. After that, look in the solution explorer, right click the folder that says “Source Files” and click add then click “New Item”. Name it “Main.cpp”. Now just copy and paste my code in that blank page.
That works Excellent!!!
So, what kinds of improvements are you looking for???
One Player against the Computer??
Vexer
June 29, 2009, 9:38am
11
Thanks!!! Eventually I want to program an AI (Computer Player) so that you can play 2 player or 1 player with a computer. I’m also thinking about creating my own portable game system with an LED matrix so, I could modify this code so that it is compatible with an AVR. If you make any modifications to the code could you please post the new code?
I criticize you for making your code shorter than mine! You shouldn’t be using that kind of experience
Back when I was first working with C++, the first things I made were connect four and tic-tac-toe- my TTT easily went on 600 lines +
Good job on this.
Vexer
July 1, 2009, 12:48pm
13
Thanks! Do you still have your original c++ code for tic-tac-toe?
Vexer
July 8, 2009, 9:16pm
14
I’ve been looking up ways to make an ai for tic-tac-toe, I’ve seen some people suggest to use minmax .I think Im just going to stick with the old fashioned:eek: if statement.