This is my first time programming in c++, so don't roast me too hard lol. Anyways it took me about an hour, but here it is. If you guys do not mind could you give me some pointers on how to improve it. Thanks!
Code
# include <iostream>
using namespace std;
bool gameOver;
class Board{
private:
string board[3][3] = {{"_","_","_"},{"_","_","_"},{"_","_","_"}};
public:
void draw(){
system("cls");
cout << "The top left position is 0 then 1 then 2 all the way to 8" << endl;
for(int row = 0; row < 3; row++){
for(int column = 0; column < 3; column++){
cout << board[row][column] << " ";
}
cout << endl;
}
}
bool checkWin(string playerChar){
if(playerChar == board[0][0] && playerChar == board[0][1] && playerChar == board[0][2]){
return true;
}
if(playerChar == board[1][0] && playerChar == board[1][1] && playerChar == board[1][2]){
return true;
}
if(playerChar == board[2][0] && playerChar == board[2][1] && playerChar == board[2][2]){
return true;
}
if(playerChar == board[0][0] && playerChar == board[1][0] && playerChar == board[2][0]){
return true;
}
if(playerChar == board[0][1] && playerChar == board[1][1] && playerChar == board[2][1]){
return true;
}
if(playerChar == board[0][2] && playerChar == board[1][2] && playerChar == board[2][2]){
return true;
}
if(playerChar == board[0][0] && playerChar == board[1][1] && playerChar == board[2][2]){
return true;
}
if(playerChar == board[0][2] && playerChar == board[1][1] && playerChar == board[2][0]){
return true;
}
}
void updateBoard(string playerChar, int row, int column){
board[row][column] = playerChar;
}
};
int main(){
int row;
int column;
Board board;
gameOver = false;
while(!gameOver){
board.draw();
cout << "Player 1 row: ";
cin >> row;
cout << endl << "Player 1 column ";
cin >> column;
board.updateBoard("X", row, column);
if(board.checkWin("X") == true){
board.draw();
cout << endl << "Player 1 wins!!!!!!!!!!!";
gameOver = true;
break;
}
board.draw();
cout << "Player 2 row: ";
cin >> row;
cout << endl << "Player 2 column ";
cin >> column;
board.updateBoard("O", row, column);
if(board.checkWin("O") == true){
board.draw();
cout << endl << "Player 1 wins!!!!!!!!!!!";
gameOver = true;
break;
}
}
return 0;
}
