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 157 158 159 160 161 162 163 164 165
|
// ------------------------------------------------------------------
// Purpose: Write a C++ program to display a 2-dimensional tic-tac-toe
// board and manage a game played by two humans with keyboard //input.
//
//
// ------------------------------------------------------------------
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void NewGame(char[4][4]);
void Progress(char[4][4], int&);
void Winner(char[4][4], char, int);
int main()
{
//-| ----------------------------------------------------------------------
//-| Declare variables
//-| ----------------------------------------------------------------------
char Moves[4][4] = {
{ 'B', 'B', 'B', 'B'},
{ 'B', 'B', 'B', 'B'},
{ 'B', 'B', 'B', 'B'},
{ 'B', 'B', 'B', 'B'}}; // Moves on board
int count=0; // Move count
//-| ----------------------------------------------------------------------
//-| Print the copyright notice declaring authorship.
//-| ----------------------------------------------------------------------
cout << endl << "(c) 2013, jtorrence Jared Torrence, tpetit Travon Petit, bfields Byron Fields" << endl << endl;
//-| ----------------------------------------------------------------------
//-| 1. Display New Game Board
//-| ----------------------------------------------------------------------
NewGame(Moves);
//-| ----------------------------------------------------------------------
//-| 2. Play Game
//-| ----------------------------------------------------------------------
Progress(Moves, count);
//-| ----------------------------------------------------------------------
//-| 3. Check to See if Winner is X
//-| ----------------------------------------------------------------------
Winner(Moves, 'X', count);
//-| ----------------------------------------------------------------------
//-| 4. Check to See if Winner is O
//-| ----------------------------------------------------------------------
Winner(Moves, 'O', count);
//-| ----------------------------------------------------------------------
//-| Print the copyright notice declaring authorship again.
//-| ----------------------------------------------------------------------
cout << endl << "(c) 2013, jtorrence Jared Torrence, tpetit Travon Petit, bfields Byron Fields" << endl << endl;
system("pause");
return 0;
}//main
void NewGame(char Moves[4][4])
{
for(int i=1;i<4;i++)
{
for(int k=1;k<4;k++)
{
if(Moves[i][k] == 'B')
Moves[i][k] = ' ';
}//for
}//for
cout << " ==> GAME BOARD:\n";
cout << " ---+---+---\n";
cout << " " << ' ' << Moves[1][1] << " | " << Moves[1][2] << " | " << Moves[1][3] << " \n";
cout << " ---+---+---\n";
cout << " " << ' ' << Moves[2][1] << " | " << Moves[2][2] << " | " << Moves[2][3] << " \n";
cout << " ---+---+---\n";
cout << " " << ' ' << Moves[3][1] << " | " << Moves[3][2] << " | " << Moves[3][3] << " \n";
cout << " ---+---+---\n\n";
cout << " ==> GAME STATE: NEW GAME\n\n\n";
}//New Game
void Progress(char Moves[4][4], int & count)
{
int moverow; // Row on board
int movecol; // Column on board
count++;
if(count%2 != 0)
{
cout << "Player X, Enter move (row column): ";
cin >> moverow >> movecol;
Moves[moverow][movecol] = 'X';
}//if
else
{
cout << "Player O, Enter move (row column): ";
cin >> moverow >> movecol;
Moves[moverow][movecol] = 'O';
}//else
for(int i=1;i<4;i++)
{
for(int k=1;k<4;k++)
{
if(Moves[i][k] == 'B')
Moves[i][k] = ' ';
}//for
}//for
cout << "\n\n ==> GAME BOARD:\n";
cout << " ---+---+---\n";
cout << " " << ' ' << Moves[1][1] << " | " << Moves[1][2] << " | " << Moves[1][3] << " \n";
cout << " ---+---+---\n";
cout << " " << ' ' << Moves[2][1] << " | " << Moves[2][2] << " | " << Moves[2][3] << " \n";
cout << " ---+---+---\n";
cout << " " << ' ' << Moves[3][1] << " | " << Moves[3][2] << " | " << Moves[3][3] << " \n";
cout << " ---+---+---\n\n";
cout << " ==> GAME STATE: IN PROGRESS\n";
}//Progress
void Winner(char Moves[4][4], char Player, int moves)
{
bool win = false; // win is a flag
if(Moves[1][1] == Player && Moves[2][2] == Player && Moves[3][3] == Player)
win = true;
else if(Moves[1][1] == Player && Moves[1][2] == Player && Moves[1][3] == Player)
win = true;
else if(Moves[1][1] == Player && Moves[2][1] == Player && Moves[3][1] == Player)
win = true;
else if(Moves[2][1] == Player && Moves[2][2] == Player && Moves[2][3] == Player)
win = true;
else if(Moves[3][1] == Player && Moves[3][2] == Player && Moves[3][3] == Player)
win = true;
else if(Moves[1][2] == Player && Moves[2][2] == Player && Moves[3][2] == Player)
win = true;
else if(Moves[1][3] == Player && Moves[2][3] == Player && Moves[3][3] == Player)
win = true;
else if(Moves[1][3] == Player && Moves[2][2] == Player && Moves[3][1] == Player)
win = true;
if(win)
{
for(int i=1;i<4;i++)
{
for(int k=1;k<4;k++)
{
if(Moves[i][k] == 'B')
Moves[i][k] = ' ';
}//for
}//for
cout << " ==> GAME BOARD:\n";
cout << " ---+---+---\n";
cout << " " << ' ' << Moves[1][1] << " | " << Moves[1][2] << " | " << Moves[1][3] << " \n";
cout << " ---+---+---\n";
cout << " " << ' ' << Moves[2][1] << " | " << Moves[2][2] << " | " << Moves[2][3] << " \n";
cout << " ---+---+---\n";
cout << " " << ' ' << Moves[3][1] << " | " << Moves[3][2] << " | " << Moves[3][3] << " \n";
cout << " ---+---+---\n\n\n";
cout << " ==> GAME STATUS: WINNER IS " << Player << endl << endl;
cout << " ==> GAME COMPLETED IN " << moves << " MOVES" << endl << endl;
}//if win
}//Winner
|