Hey guys. I'm making a program for tic tac toe but i'm stuck now.
I want to move the section in the while loop from the main function at the very bottom to a new public class called nextMove but I'm not sure how to do that. Also How can I move my gameboard up to the TicTacToe constructor? I tried it but it kept messing up. Thank you!
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#include "tic_tac_toe.h"
TicTacToe::TicTacToe()
{
row = 0;
column = 0;
player = 1;
int i = 0;
int j = 0;
for ( i = 0; i < 3; i++)
{
for ( j = 0; j < 3; j++)
{
board[i][j] = 0;
display_board[i][j] = ' ';
}
}
}
void TicTacToe::ChooseRow(int r)
{
row = r;
}
void TicTacToe::ChooseColumn(int c)
{
column = c;
}
bool TicTacToe::setValue(int row, int column, int player)
{
row--;
column--;
cout << endl << row <<" "<< column;
if( row>2 || row<0 || column>2 || column<0)
return false;
if(board[row][column] == '\0' || board[row][column] == 'X' || board[row][column] == 'O')
return false;
if(player == 1)
board[row][column] = 'X';
else
board[row][column] = 'O';
TotalMoves++;
return true;
}
bool TicTacToe:: validmove (int row, int column)
{
if ( row != 0 && row != 1 && row != 2 )
{
cout << " Invalid choice!!";
cout << endl;
return 0;
}
else if ( column != 0 && column != 1 && column != 2 )
{
cout << " Invalid choice!! " << endl;
return 0;
}
else if ( board[row][column] == 1 || board[row][column] == 2)
{
cout << " Space already used. Try Again. " << endl;
return 0;
}
else
{
board[row][column] = player;
return 1;
}
} // end of Check_Move
int TicTacToe::CheckBoard()
{
int i = 0;
int j = 0;
int sum = 0;
int test = 0;
int count = 0;
for (i = 0; i < 3; i++)
{
sum = 0;
for ( j = 0; j < 3; j++)
{
if (board[i][j] == 0)
{
count++;
}
sum += (board[i][j] * board[i][j]);
}
if ( sum == 3 || sum == 12)
{
test = sum;
break;
}
sum = 0;
} // end of for loop
for ( j = 0; j < 3; j++)
{
sum = 0;
for ( i = 0; i < 3; i++)
{
sum += (board[i][j] * board[i][j]);
}
if ( sum == 3 || sum == 12)
{
test = sum;
break;
}
sum = 0;
} // end of for loop
if ( test != 3 || test != 12)
{
sum = (board[0][0] * board[0][0])+ (board[1][1] * board[1][1]) + (board[2][2] * board[2][2]);
if ( sum == 3 || sum == 12)
{
test = sum;
}
} // end of if condition
if (test != 3 || test != 12)
{
sum = (board[2][0] * board[2][0])+ (board[1][1] * board[1][1]) + (board[0][2] * board[0][2]);
if ( sum == 3 || sum == 12 )
{
test = sum;
}
} // end of if condition
if ( test == 3)
{
test = 1;
}
else if ( test == 12)
{
test = 2;
}
else if ( count == 0)
{
test = 3;
}
else
{
test = 0;
}
return test;
} // end of Check_Board
void TicTacToe:: Board()
{
for ( int row = 0; row < 3; row ++)
{
for ( int column = 0; column < 3; column++)
{
if ( board[row][column] == 0)
{
display_board[row][column] = ' ';
}
if ( board[row][column] == 1)
{
display_board[row][column] = 'X';
}
if ( board[row][column] == 2)
{
display_board[row][column] = 'O';
}
} // end of inner for loop
} // end of outer for loop
cout << " Welcome to Tic-Tac-Toe " << endl;
cout << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << display_board[0][0] << " | " <<
display_board[0][1] << " | " << display_board[0][2] << " " << endl;
cout << " | | " << endl;
cout << "------------------------------" << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << display_board[1][0] << " | " <<
display_board[1][1] << " | " << display_board[1][2] << " " << endl;
cout << " | | " << endl;
cout << "------------------------------" << endl;
cout << " | | " << endl;
cout << display_board[2][0] << " | " <<
display_board[2][1] << " | " << display_board[2][2] << " " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
} // end of Tic_Tac_Toe_Board
Player::Player(char name[], int index)
{
strcpy(PlayerName, name);
PlayerId = index;
}
int Player::GetIndex()
{
return PlayerId;
}
char* Player::GetName()
{
return PlayerName;
}
void Player::NextMove(TicTacToe & board)
{
int i=0, row=-1, col=-1;
char move[3];
while(PlayerName[i] != '\0')
{
cout << PlayerName[i];
i++;
}
cout << "Make a move: ";
cin >> row >> col;
while(!board.setValue(row, col, PlayerId))
{
cout << endl << "Illegal move, make another one: ";
cin >> row >> col;
}
}
int main()
{
TicTacToe game;
bool test;
bool more = true;
int row = 0;
int column= 0;
int player;
int check = 0;
char player1;
char player2;
TicTacToe();
while ( more )
{
game.Board();
player = game.PickPlayer();
cout << " Current Player " << player;
cout << endl;
cout << " Make a move: " ;
cin >> row >> column;
game.ChooseRow(row);
game.ChooseColumn(column);
test = game.validmove( game.PickRow(), game.PickColumn());
if ( test == 1)
{
check = game.CheckBoard();
}
else
{
while ( test == 0 )
{
cout << " Current Player " << game.PickPlayer() <<" Invalid Choice" << endl;
cout << " Make a move: " ;
cin >> row >> column;
cout << endl;
game.ChooseRow(row);
game.ChooseColumn(column);
test = game.validmove(game.PickRow(),game.PickColumn());
} // end of while loop
check = game.CheckBoard();
}
if ( check == 1 || check == 2)
{
break;
}
else if ( check == 3 )
{
game.Board();
cout << " The game is tied. " << endl;
}
if ( player == 1)
{
player = 2;
}
else
{
player = 1;
}
game.PlayerChoice(player);
} // end of outer while loop
game.Board();
cout << " Player " << check << " wins. " << endl;
return 0;
}
Here is my header file
#include <iostream>
using namespace std;
class TicTacToe
{
public:
TicTacToe(); // constructor
int PickPlayer() {return player;}
int PickRow() {return row;}
int PickColumn() {return column;}
void PlayerChoice(int p) { player = p;}
void ChooseRow(int);
void ChooseColumn(int);
bool setValue ( int row, int column, int player );
bool validmove(int row, int column);
void Board();
int CheckBoard();
int GetStatus();
void TicTacToeBoard();
private:
int row;
int column;
int player;
int board[3][3];
char display_board[3][3];
int GameStatus;
int TotalMoves;
};
class Player
{
public:
Player(char name[], int index);
int GetIndex();
char* GetName();
void NextMove(TicTacToe & board);
private:
char PlayerName[15];
int PlayerId;
};