#include <iostream>
using namespace std;
void CreateCharBoard(int Board[][3], char CharBoard[][3]);
void PrintBoard(char CharBoard[][3]);
bool CheckWinner(int Board[][3], int Player);
void ResetBoard(int Board[][3]);
bool CheckMove(int Board[][3], int Row, int Column);
int main(){
// delcare and initialize (if necessary) all required variables here
int Board[3][3];
char CharBoard[3][3];
int Player, Row, Column;
bool Win = false;
char Again = 'y';
// Print lab header and if desired, statement of what this lab is doing
//this line of code will pause the program until the user hits any key to continue
system("pause");
while (Again == 'y'){
ResetBoard(Board);
system("clear");
while (Win == false){
CreateCharBoard(Board, CharBoard);
PrintBoard(CharBoard);
cout << "Player O's turn!" << endl;
cout << "Enter row number: "; cin >> Row;
cout << "Enter column number: "; cin >> Column;
CheckMove(Board, Row, Column);
//update board
Win = CheckWinner(Board, -1);
if (CheckWinner (Board, -1) == true){
cout << "Player O wins!" << endl;
break;
}
//set flag to denote it's x's turn
PrintBoard(CharBoard);
cout << "Player X's turn!" << endl;
cout << "Enter row number: "; cin >> Row;
cout << "Enter column number: "; cin >> Column;
CheckMove(Board, Row, Column);
//update board
Win = CheckWinner (Board, 1);
if (CheckWinner (Board, 1) == true){
cout << "Player X wins!" << endl;
break;
}
}
cout << endl << endl;
cout << "Would you like to play again? (y/n): ";
cin >> Again;
}
cout << endl << endl;
cout << "Good bye!" << endl;
return 0;
}
// function to take game board and create a board with corresponding characters
void CreateCharBoard(int Board[][3], char CharBoard[][3])
{
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
// if game board has a -1, put an "O" in the character board
if (Board[i][j]== -1){
CharBoard[i][j] = 'O';
}
// if game board has a 1, put an "X" in the character board
else if (Board[i][j]== 1){
CharBoard[i][j] = 'X';
}
// if game board has a 0, put an " " (blank space) in the character board
else{
CharBoard[i][j] = ' ';
}
}
}
}
// print the character representation of the game board
void PrintBoard(char CharBoard[][3]){
cout << " Tic Tac Toe!" << endl << endl;
cout << " 0 1 2" << endl << endl;
开发者_JS百科 cout << "0 " << CharBoard[0][0] << " | " << CharBoard[0][1] << " | " << CharBoard[0][2] << endl;
cout << " -----------------" << endl;
cout << "1 " << CharBoard[1][0] << " | " << CharBoard[1][1] << " | " << CharBoard[1][2] << endl;
cout << " -----------------" << endl;
cout << "2 " << CharBoard[2][0] << " | " << CharBoard[2][1] << " | " << CharBoard[2][2] << endl << endl;
}
// function to check if a player, either "X" or "O" has won the game
bool CheckWinner(int Board[][3], int Player){
int Sum;
// check each column
for (int i = 0; i < 3; i++){
Sum = Board[i][0] + Board[i][1] + Board[i][2];
// if O is a winner
if (Sum == 3*Player){
return true;
}
}
// check each row
for (int j = 0; j < 3; j++){
Sum = Board[0][j] + Board[1][j] + Board[2][j];
// if O is a winner
if (Sum == 3*Player){
return true;
}
}
// check diagonal
Sum = Board[0][0] + Board[1][1] + Board[2][2];
if (Sum == 3*Player){
return true;
}
Sum = Board[0][2] + Board[1][1] + Board[2][0];
if (Sum == 3*Player){
return true;
}
// if none of the rows, columns, or diagonals = 3*player, then return false
return false;
}
// function to reset all elements of the game board back to 0
void ResetBoard(int Board[][3]){
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
Board[i][j] = 0;
}
}
}
// function to see if player's move is valid, if 0 (or not played) return true, otherwise false
bool CheckMove(int Board[][3], int Row, int Column){
if ((Row < 0) || (Row > 2)){
return false;
}
else if ((Column < 0) || (Column > 2)){
return false;
}
else if (Board[Row][Column] == 0){
return true;
}
else{
return false;
}
}
Looks like you newer update your Board
.
The only line where you write anything into Board
is in ResetBoard()
where you reset all values to 0
.
In your game-loop are two lines // update board
- I suppose you forgot to implement the function for that...
Other flaws:
You call
CheckWinner
twice - once to setWin
and once for the check, if user has won.The return of
CheckMove
is not used - there should be an loop that reads input till the player enters valid data.If one player has won you end the
while
-loop by break. The checkwhile (Win == false)
is never used and to show that to everybody it should bewhile(true)
.
精彩评论