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
|
#include <iostream>
int printboard(char board[9]);
int wincheckfunc(char board[9], int wincheck,int playmore);
int input(int errorfree,int placement, char board[9]);
void endgame(int playmore, char board[9], int turn);
int main() {
char board[9] = {'0','1','2','3','4','5','6','7','8'};
int wincheck = 0;
int playmore = 1;
int placement = 0;
int turn = 1;
int errorfree = 1;
printboard(board);
for (int i = 0; i < 4; i++)
{
std::cout << "Player 1's turn! Where do you want to place your X?" << "\n";
placement = input(errorfree,placement,board);
errorfree = 1; // Resets 'errorchecking' variable so that the next input will (also) be checked.
board[placement] = 'X';
playmore = wincheckfunc(board,wincheck,playmore);
if (playmore == 0) break;
turn++; // This turn counter that ticks back and forth between 1 and 2, determines which player will be said to have won.
printboard(board);
if (i == 4) break;
std::cout << "Player 2's turn! Where do you want to place your O?" << "\n";
placement = input(errorfree,placement,board);
errorfree = 1; // Resets 'errorchecking' variable so that the next input will also be checked.
board[placement] = 'O';
playmore = wincheckfunc(board,wincheck,playmore);
if (playmore == 0) break;
turn--; // This turn counter that ticks back and forth between 1 and 2, determines which player will be said to have won.
printboard(board);
}
endgame(playmore,board,turn);
system ("pause");
return 0;
}
int printboard(char board[9])
{
system("cls");
std::cout << "\n";
std::cout << " " << board[0] << " " << board[1] << " " << board[2] << " " << "\n";
std::cout << "\n";
std::cout << " " << board[3] << " " << board[4] << " " << board[5] << " " << "\n";
std::cout << "\n";
std::cout << " " << board[6] << " " << board[7] << " " << board[8] << " " << "\n";
std::cout << "\n";
return 0;
}
int wincheckfunc(char board[9], int wincheck, int playmore)
{
if (board[0] == board[1] && board[1] == board[2]) // Horizontal 1
playmore = 0;
else if (board[3] == board[4] && board[4] == board[5]) // Horizontal 2
playmore = 0;
else if (board[6] == board[7] && board[7] == board[8]) // Horizontal 3
playmore = 0;
else if (board[0] == board[3] && board[3] == board[6]) // Vertical 1
playmore =0;
else if (board[1] == board[4] && board[4] == board[7]) // Vertical 2
playmore = 0;
else if (board[2] == board[5] && board[5] == board[8]) // Vertical 3
playmore = 0;
else if (board[0] == board[4] && board[4] == board[8]) // Diagonal 1
playmore = 0;
else if (board[2] == board[4] && board[4] == board[6]) // Diagonal 2
playmore = 0;
else if (board[0] != '0' && board[1] != '1' && board[2] != '2' && board[3] != '3' && board[4] != '4' && board[5] != '5' && board[6] != '6 '&& board[7] != '7' && board[8] != '8') // Draw
playmore = 0;
return playmore;
}
int input(int errorfree,int placement, char board[9])
{
while (errorfree == 1) // "std::cin placement;" with errorchecking
{ // that nothing is placed there from earlier turns.
std::cin >> placement; //
errorfree = 0; //
if (board[placement] == 'X') { //
errorfree = 1; //
std::cout << "Error. You have already placed an X there! Try again." << "\n"; } //
else if (board[placement] == 'O') { //
errorfree = 1; //
std::cout << "Error. You have already placed an O there! Try again." << "\n"; }//
}
return placement;
}
void endgame(int playmore, char board[9], int turn)
{
printboard(board);
if (playmore == 0)
{
std::cout << "\n";
std::cout << "Congratulations, player " << turn << " wins!" << "\n";
}
else std::cout << "It's a draw. Game over." << "\n";
}
|