You have probably seen my other thread for the development of my little game, and now YOU can play it!!!
This isn't the most optimised code ever, but I gave a shot at arrays, and as long as the game works, and the code looks pretty neat, its fine by me.
WARNING! GAME HAS A FLASHING SCREEN UPON FINISH!
So, here goes nothing:
// Tic_Tac_Toe.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "windows.h"
#include <string>
using namespace std;
string start;
int x;
int y;
const char* player;
string turn;
const char* board[3][3];
bool turn_x = true;
int main()
{
cout << "Welcome to 2 player Tic Tac Toe!" << endl;
cout << "Use Num-Pad syntax! E.g. 7, 2, etc..." << endl << endl;
cout << "Type 'HELP' or 'INFO' or 'START'" << endl << endl << endl;
while(1)
{
cin >> start;
cout << endl << endl;
if(start == "HELP")
{
cout << "Use Num-Pad syntax! E.g." << endl << "789\n456\n123" << endl << endl;
}
if(start == "INFO")
{
cout << "Created by Lukeme99 using VCE2010" << endl << endl;
}
if(start == "START")
{
break;
}
}
cout << "Starting Game!" << endl << endl;
Sleep(500);
for( x = 0; x < 3; x++ )
{
for( y = 0; y < 3; y++ )
{
board[x][y] = "/"; // Set each element to a value
}
}
while(1)
{
if(turn_x)
{
player = "X";
}
else
{
player = "O";
}
for ( x = 0; x < 3;x++ )
{
for ( y = 0; y < 1; y++ )
{
cout << board[x][y];
}
for ( y = 1; y < 2; y++ )
{
cout << board[x][y];
}
for ( y = 2; y < 3; y++ )
{
cout << board[x][y];
}
cout << endl;
}
cout << endl;
while(1)
{
cin >> turn;
if(turn == "7" && board[0][0] == "/")
{
board[0][0] = player;
break;
}
else if(turn == "8" && board[0][1] == "/")
{
board[0][1] = player;
break;
}
else if(turn == "9" && board[0][2] == "/")
{
board[0][2] = player;
break;
}
else if(turn == "4" && board[1][0] == "/")
{
board[1][0] = player;
break;
}
else if(turn == "5" && board[1][1] == "/")
{
board[1][1] = player;
break;
}
else if(turn == "6" && board[1][2] == "/")
{
board[1][2] = player;
break;
}
else if(turn == "1" && board[2][0] == "/")
{
board[2][0] = player;
break;
}
else if(turn == "2" && board[2][1] == "/")
{
board[2][1] = player;
break;
}
else if(turn == "3" && board[2][2] == "/")
{
board[2][2] = player;
break;
}
}
cout << endl;
if( (board[0][0] == "X" && board[0][1] == "X" && board[0][2] == "X") ||
(board[1][0] == "X" && board[1][1] == "X" && board[1][2] == "X") ||
(board[2][0] == "X" && board[2][1] == "X" && board[2][2] == "X") ||
(board[0][0] == "X" && board[1][0] == "X" && board[2][0] == "X") ||
(board[0][1] == "X" && board[1][1] == "X" && board[2][1] == "X") ||
(board[0][2] == "X" && board[1][2] == "X" && board[2][2] == "X") ||
(board[0][0] == "X" && board[1][1] == "X" && board[2][2] == "X") ||
(board[0][2] == "X" && board[1][1] == "X" && board[2][0] == "X") )
{
for ( x = 0; x < 3;x++ )
{
for ( y = 0; y < 1; y++ )
{
cout << board[x][y];
}
for ( y = 1; y < 2; y++ )
{
cout << board[x][y];
}
for ( y = 2; y < 3; y++ )
{
cout << board[x][y];
}
cout << endl;
}
cout << "Well Done" << endl;
cout << "Player 1 is the winner!" << endl;
Sleep(1000);
while(1)
{
system("COLOR 2");
cout << "X Won!" << endl;
Sleep(100);
system("COLOR F2");
cout << "X Won!" << endl;
Sleep(100);
}
}
if( (board[0][0] == "O" && board[0][1] == "O" && board[0][2] == "O") ||
(board[1][0] == "O" && board[1][1] == "O" && board[1][2] == "O") ||
(board[2][0] == "O" && board[2][1] == "O" && board[2][2] == "O") ||
(board[0][0] == "O" && board[1][0] == "O" && board[2][0] == "O") ||
(board[0][1] == "O" && board[1][1] == "O" && board[2][1] == "O") ||
(board[0][2] == "O" && board[1][2] == "O" && board[2][2] == "O") ||
(board[0][0] == "O" && board[1][1] == "O" && board[2][2] == "O") ||
(board[0][2] == "O" && board[1][1] == "O" && board[2][0] == "O") )
{
for ( x = 0; x < 3;x++ )
{
for ( y = 0; y < 1; y++ )
{
cout << board[x][y];
}
for ( y = 1; y < 2; y++ )
{
cout << board[x][y];
}
for ( y = 2; y < 3; y++ )
{
cout << board[x][y];
}
cout << endl;
}
cout << "Well Done" << endl;
cout << "Player 2 is the winner!" << endl;
Sleep(1000);
while(1)
{
system("COLOR 2");
cout << "O Won!" << endl;
Sleep(100);
system("COLOR F2");
cout << "O Won!" << endl;
Sleep(100);
}
}
if(board[0][0] != "/" && board[0][1] != "/" && board[0][2] != "/" && board[1][0] != "/" && board[1][1] != "/" && board[1][2] != "/" && board[2][0] != "/" && board[2][1] != "/" && board[2][2] != "/")
{
for ( x = 0; x < 3;x++ )
{
for ( y = 0; y < 1; y++ )
{
cout << board[x][y];
}
for ( y = 1; y < 2; y++ )
{
cout << board[x][y];
}
for ( y = 2; y < 3; y++ )
{
cout << board[x][y];
}
cout << endl;
}
cout << "GAME OVER!" << endl;
cout << "There Are NO Winners!" << endl;
Sleep(1000);
while(1)
{
system("COLOR 2");
cout << "GAME OVER!" << endl;
Sleep(100);
system("COLOR F2");
cout << "GAME OVER!" << endl;
Sleep(100);
}
}
if(turn_x)
{
turn_x = false;
}
else
{
turn_x = true;
}
}
}
There you go,
Have FUN!
Thanks for playing,
L99

New Topic/Question
Reply



MultiQuote





|