1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
class Board
{
//access specifier
public:
//Global constants for the game pieces
// const char empty = '-'; // do i need this for the line that separates numbers and game board?
const char empty = '*'; // empty/available places
const char P1 = 'X'; // player 1
const char P2 = 'O'; // player 2
//global variables/constants for the game board
const int col = 3;
const int row = 5;
/*
const rows { 3 };
const cols { 3 };
char arr[rows][cols];
*/
//Initialize the board to be empty
void initializeBoard();
//Display the board
void displayBoard();
//Get column for current move
int getColumn(bool isPlayer1);
//Add one piece to the Board
bool addPiece(int c, bool isPlayer1);
//Check if a given player has won
bool isWinner(bool isPlayer1, int lastCol);
// how do I check for a tie in the game?
private:
// the private is the 2d array and you can set elements on the 2darray
// should only be able to set elements on 3 bottom rows
char array2D[5][3]
{
{'1', '2', '3'},
{'-', '-', '-'},
{'*', '*', '*'},
{'*', '*', '*' },
{'*', '*', '*'}
};
};
int main() {
playerInfo playerOne, playerTwo;
// intro to game
cout << "Let's Play Connect 3. Below is the initial game board.\n"
<<"Select column number 1, 2, 3; along the top; to indicate your move.\n"
<< endl;
// this prints the array but what is a more efficient way i can print the empty board game
//here as well as an updated array every time a player makes a move?
char array2D[5][3]
{
{'1', '2', '3'},
{'-', '-', '-'},
{'*', '*', '*'},
{'*', '*', '*' },
{'*', '*', '*'}
};
for (int row{}; row<5; ++row)
{
for (int col{}; col<3; ++col)
{
cout<<array2D[row][col];
}
cout<<endl;
}
// menu choice
enum menu {otherPlayer = 'o', computer = 'c', quit = 'q'};
char i;
cout << "\nChoose who you would like to play with: o)ther player, c)omputer, q)uit: ";
cin >> i;
menu userChoice {};
userChoice = static_cast<menu>(i);
switch(userChoice)
{
case otherPlayer:
cout << "You picked to play with another player\n"
<< "Player One please enter your name: ";
cin >> playerOne.playerName;
playerOne.playerID = 'X';
cout << "Player Two please enter your name: ";
cin >> playerTwo.playerName;
playerTwo.playerID = 'O';
cout <<"The two player tokens are X and O. Player 1 (X) begins.\n";
//This is the part I am stuck on....
// here I will print the array/modified array based on plays
/*after a game is finished print:
Congratulations! Player 1(X) has won the game!!!
Do you want to play again? (y/n): n
Player 1 won: 1 times. 100.00% Player 2 won: 0 times. 0.00%
This ends the Connect game. Goodbye.
break;
*/
case computer:
cout << "You picked to play against the computer\n";
break;
case quit:
cout << "You have forfeited the match! Would you like to play again?\n";
break;
default:
cout << "That is not an option! Try again...\n";
break;
}
return 0;
}
| |