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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
// Two player tic tac toe
#include <iostream>
using namespace std;
void displayBoard(int board[]);
char moveMade(int space, int board[]);
void getMove(int playerTurn, int board[]);
void checkLegal(int playerTurn, int move, int board[]);
//void checkWin(); Not written yet
//void winnable();
//void gameEnd();
int main()
{
int playerTurn = 1; // Starts game with player ones turn.
cout << "Player One will be 'X'. Player Two will be 'O'. First player to get three in a row wins.";
int board[10];
int spaces = 0;
for (int i = 0; i < 10; i++)
{
board[i] = spaces;
spaces++;
}
for (int i = 0; i < 9; i++)
{
displayBoard(board);
getMove(playerTurn, board);
//checkWin();
//checkWinnable();
// This is where the player swap should happen, but it is not
if (playerTurn == 1)
{
playerTurn = 2;
}
else
{
playerTurn = 1;
}
}
cin.ignore();
cin.get();
}
void displayBoard(int board[])
{
cout << "\n | |" << endl;
cout << " " << moveMade(1, board) << " | " << moveMade(2, board) << " | " << moveMade(3, board) << endl;
cout << " | |" << endl;
cout << "---+---+---" << endl;
cout << " | |" << endl;
cout << " " << moveMade(4, board) << " | " << moveMade(5, board) << " | " << moveMade(6, board) << endl;
cout << " | |" << endl;
cout << "---+---+---" << endl;
cout << " | |" << endl;
cout << " " << moveMade(7, board) << " | " << moveMade(8, board) << " | " << moveMade(9, board) << endl;
cout << " | |" << endl;
}
char moveMade(int space, int board[])
{
char moveMade;
if (board[space] == 15)
{
moveMade = 'X';
}
else if (board[space] == 25)
{
moveMade = 'O';
}
else if (board[space] == 1)
{
moveMade = 49;
}
else if (board[space] == 2)
{
moveMade = 50;
}
else if (board[space] == 3)
{
moveMade = 51;
}
else if (board[space] == 4)
{
moveMade = 52;
}
else if (board[space] == 5)
{
moveMade = 53;
}
else if (board[space] == 6)
{
moveMade = 54;
}
else if (board[space] == 7)
{
moveMade = 55;
}
else if (board[space] == 8)
{
moveMade = 56;
}
else if (board[space] == 9)
{
moveMade = 57;
}
return moveMade;
}
void getMove(int playerTurn, int board[])
{
int move;
if (playerTurn = 1)
{
cout << "Player One enter the number of the square for your move: ";
}
else {
cout << "Player Two enter the number of the square for your move: ";
}
cin >> move;
checkLegal(playerTurn, move, board);
}
void checkLegal(int playerTurn, int move, int board[])
{
if (board[move] == 15 || board[move] == 25)
{
cout << "\nThat is not a legal selection.\n";
getMove(playerTurn, board);
}
else if (playerTurn == 1)
{
board[move] = 15;
}
else if (playerTurn == 2)
{
board[move] = 25;
}
}
|