-
April 6th, 2012, 11:46 AM
#1
Help with Tic Tac Toe game code c++
hey could some please help me? I have these two codes I want to keep the second one but I want the grid to be like the first one, and few things like play again, I want to add that to second code, but moe concerned about the grid,please help?
Code:
#include<iostream>
#include<string>
#include"110ct.h"
using namespace std;
class TicTacToe
{
private:
ColourController ct;
ColourController cls;
CursorController crs;
char Board[3][3];
int play();
void playermove();
int turn(int);
int check (int, int);
void boardProgress(char x[][3]);
int Winner(char x[][3]);
int player1, player2, draw, player,winner,done;
int row, col;
public:
int y, z, t, s;
void intBoard();
int Statistics(int);
char playAgain(char);
}playBall;
int main()
{
playBall.y = 0,playBall.z = 0,playBall.t = 0,playBall.s = 0;
int Winner = 0;
ColourController ct;
ColourController cls;
cls.setForeground(Aqua);
cout << " welcome to TIC TAC TOE" <<endl;
char done = false;
while (!done)
{
ColourController cls;
cls.setForeground(Aqua);
cout << " Good Luck!"<< endl;
playBall.intBoard();
done = playBall.playAgain(done);
}
cout << "Thank you for my playing my game"<< endl;
system ("pause");
return 0;
}
void TicTacToe::intBoard()
{
done = false;
winner = 0;
player = 2;
player1 = 1;
player2 = 2;
draw = 3;
int i,j;
for (j=0;j<3;j++)
{
for (i=0;i<3;i++)
{
Board[j][i]=0;
}
}
play();
}
int TicTacToe::play()
{
int done = false;
boardProgress(Board);
while(!done)
{
playermove();
boardProgress(Board);
done = Winner(Board);
}
Statistics(winner);
return 0;
}
void TicTacToe::playermove()
{
int answer = false;
while (answer == false)
{
cout <<"Row: ";
cin >> row;
row--;
cout <<"Column: ";
cin >> col;
col--;
answer = check(row,col);
}
player = turn(player);
if (player == 1)
Board[row][col] = 'X';
else if (player == 2)
Board[row][col] = 'O';
else
cout<< "Failed.";
}
int TicTacToe::check(int row, int col)
{
if(Board[row][col] == 0)
{
return true;
}
else if (Board[row][col] != 0)
{
cout <<"You can't go there"<< endl;
return false;
}
else
{
cout<< "Going through Check";
Winner(Board);
}
return false;
}
int TicTacToe::turn(int player)
{
switch(player)
{
case 1: player = 2;
{
cout<<"It's Player 1's turn"<< endl;
break;
}
case 2: player = 1;
{
cout<<"It's Player 2's turn"<< endl;
break;
}
}
return player;
}
int TicTacToe::Winner(char x[][3])
{
winner = 0;
int count = 0;
int a = row;
int b = col;
for (a=0;a<3;a++)
{
for (b=0;b<3;b++)
{
if (Board[a][b]==0)
{
count++;
}
}
}
if (count > 0)
{
int row, col, r, c, d, r1, c1, d1;
for (row=0; row<3; row++)
{
r=0;
r1=0;
for (col=0; col<3; col++)
{
if(x[row][col]=='X')
r++;
if(x[row][col]=='O')
r1++;
if (r1==3)
{
winner=2;
}
if (r==3)
{
winner=1;
}
}
}
for (col=0; col<3; col++)
{
r=0;
r1=0;
for (row=0; row<3; row++)
{
if(x[row][col]=='X')
r++;
if(x[row][col]=='O')
r1++;
if (r1==3)
{
winner=2;
}
if (r==3)
{
winner=1;
}
}
}
if (x[0][0]=='X' && x[1][1]=='X' && x[2][2]=='X')
{
winner=1;
}
else if (x[0][0]=='O' && x[1][1]=='O' && x[2][2]=='O')
{
winner=2;
}
else if (x[2][0]=='X' && x[1][1]=='X' && x[0][2]=='X')
{
winner=1;
}
else if (x[2][0]=='O' && x[1][1]=='O' && x[0][2]=='O')
{
winner=2;
}
}
else if (count == 9)
{
cout << "Its a draw" << endl;
winner = 3;
}
else
{
cout<< "next player's turn"<< endl;
}
if (winner > 0)
{
done = true;
}
return done;
}
int TicTacToe::Statistics(int winner)
{
playBall.y++;
switch(winner)
{
case 1: z++;
{
break;
}
case 2: t++;
{
break;
}
case 3: s++;
{
break;
}
}
cout<<" Number of games you have played: " << y;
cout<<" Number of games Player 1 has won: " << z << " games.";
cout<<" Number of games Player 2 has won: " << t << " games.";
cout<<" Number have draws you had: " << s << " draws.";
return 0;
}
char TicTacToe::playAgain(char done)
{
cout <<"Would you like to play again"<< endl;
cout <<"Y/N: " << endl;
cin >> done;
if(done == 'Y' || done == 'y')
{
done = false;
}
else
{
done = true;
}
return done;
}
void TicTacToe::boardProgress(char x[][3])
{
cls.setForeground(Purple);
cout << " | | \n";
cout << " " << x[0][0] << " | " << x[0][1] << " | " << x[0][2] <<" \n";
cout << " _____|_____|_____\n";
cout << " | | \n";
cout << " " << x[1][0] << " | " << x[1][1] << " | " << x[1][2] <<" \n";
cout << " _____|_____|_____\n";
cout << " | | \n";
cout << " " << x[2][0] << " | " << x[2][1] << " | " << x[2][2] <<" \n";
cout << " | | \n";
} [/HTML]
second code:
[HTML]#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <iostream>
using namespace std;
class TicTacToe
{
private:
int Position[9];
int Player;
int TurnCounter;
int Players;
bool PlayerType[20];
public:
TicTacToe();
void PrintTurn();
bool PlayerHuman();
void HumanMove();
void ComputerMove();
void DrawBoard();
bool Winner();
bool FullBoard();
void NextTurn();
};
TicTacToe::TicTacToe()
{
srand(time(0));
Player = 1;
TurnCounter = 0;
int i = 0;
//Set up New Players when game begins
Players = 2;
PlayerType[1] = 1; //PlayerType[Player] is the Human User(1)
PlayerType[2] = 0; //PlayerType[Player] is the Computer (0)
for ( i = 0; i < 9; i++)
{
Position[i] = 0;
}
}
void TicTacToe::DrawBoard()
{
cout << endl
<< Position[0] << " | " << Position[1] << " | " << Position[2]
<< "\n--+---+--\n"
<< Position[3] << " | " << Position[4] << " | " << Position[5]
<< "\n--+---+--\n"
<< Position[6] << " | " << Position[7] << " | " << Position[8]
<< endl;
} //End Drawing the TicTacToe Board
void TicTacToe::PrintTurn()
{
cout << "\nPlayer " << Player << "'s turn.\n";
} //End Printing Turn
void TicTacToe::NextTurn()
{
TurnCounter++;
if (++Player > Players)
{
Player = 1;
}
} //End Next Turn
bool TicTacToe::PlayerHuman()
{
return PlayerType[Player];
} //End Determination of A Human Player
void TicTacToe::HumanMove()
{
cout << "\nEnter your move (1-9): ";
int Move;
do
{
cin >> Move;
Move--; // For user to enter 1-9 instead of 0-8
}
while (Move < 0 || Move > 8 || Position[Move] != 0);
Position[Move] = Player;
} //End the Human Move
void TicTacToe::ComputerMove()
{
int Move;
do
{
Move = rand() % 9; //Not really a Smart AI, Just picks a random move
}
while (Move < 0 || Move > 8 || Position[Move] != 0);
Position[Move] = Player;
} //End the Computer Move
bool TicTacToe::Winner()
{
int i = 0;
int Board[8][3] = {{0,1,2},
{3,4,5},
{6,7,8},
{0,3,6},
{1,4,7},
{2,5,8},
{0,4,8},
{2,4,6}}; //List of all possible winning outcomes
for ( i = 0; i < 8; i++) //Loop for all possible winning outcomes
{
if ((Position[Board[i][0]] == Position[Board[i][1]]) && (Position[Board[i][1]] == Position[Board[i][2]]) && Position[Board[i][0]] != 0)
{
cout << "\nPlayer " << Position[Board[i][0]]
<< " wins!\n\n";
cout << "Press Enter to Close...." << endl;
system("pause");
return 1; //Returns a true value (1 with the bool data type).
}
}
return 0; //Else a winner has not been found yet, and return a false (0) value
} //End Checking the Winner
bool TicTacToe::FullBoard()
{
if (TurnCounter == 9)
{
cout << "\nTie game!\n\n";
return 1;
}
else
{
return 0;
}
} //End Checking for a Full Board
int main(void)
{
TicTacToe NewGame;
cout << "The Game Board is step up in the following manner:" << endl << endl
<< " 1 | 2 | 3 " << endl
<< "---+---+---" << endl
<< " 4 | 5 | 6 " << endl
<< "---+---+---" << endl
<< " 7 | 8 | 9 " << endl << endl;
NewGame.DrawBoard();
do
{
NewGame.PrintTurn();
if (NewGame.PlayerHuman()) //If it is the Human turn, allow the human to move
{
NewGame.HumanMove();
}
else //Else if it is not the Human turn, it is the computer's turn
{
NewGame.ComputerMove();
}
NewGame.DrawBoard();
NewGame.NextTurn();
}
while (!NewGame.Winner() && !NewGame.FullBoard()); //Execute the code in the do loop while there is no winner
//And the game board is not full
return 0;
}
-
April 6th, 2012, 05:26 PM
#2
Re: Help with Tic Tac Toe game code c++
Frankly, I'm well past a good friday meal with wine and all so posting anything regarding your code would be stupid. I must comment though that you use code tags in your very first post! That's admirable, most new users don't bother reading the board info that thoroughly.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|