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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
//Include the following libraries:
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
//Using the standard namespace
using namespace std;
//Declare the following variables
char TheBoard [3][3];
int board [3][3];
void PlayOne ();
void SwitchPlayer (char &);
void ShowBoard ();
void PostMove (int, int, char);
char determineWinner ();
char CurrentPlayer='O';
int row= 0;
int clmn= 0;
//Initialize the array contents
void TicTacToe ()
{
//Declare the following local variables
int NumRows=2;
int NumCol=2;
for (row=0; row < NumRows; row++)
for (clmn=0; clmn < NumCol; clmn++)
TheBoard [row][clmn]=' ';
}
//Switches The Current Plater
void SwitchPlayer(char &tPlayer)
{
CurrentPlayer= ' ';
if (CurrentPlayer =='X')
{
cout << "\nPlayer 1: Make your move " ;
CurrentPlayer = 1;
}
else
(CurrentPlayer == 'O');
}
//Displays the Board to the user
void ShowBoard ()
{
cout<< "\n|"<<TheBoard [0][0]<<"|"<<TheBoard [0][1]<<"|"<<TheBoard [0][2]<<endl;
cout << "--------------------" << endl;
cout << "|" << TheBoard[1][0] << "|" << TheBoard[1][1] << "|" << TheBoard[1][2] << endl;
cout << "--------------------" << endl;
cout << "|" << TheBoard[2][0] << "|" << TheBoard[2][1] << "|" << TheBoard[2][2] << endl;
}
//Accepts move from the user and posts to the board
void postMove(int row, int clmn, char value)
{
char CurrentPlayer = ' ';
if (TheBoard[row][clmn]== 1|| TheBoard[row][clmn]==2)
{
cout << "Space already taken. Please choose another. " << endl;
}
else
{
TheBoard[row][clmn]=CurrentPlayer;
}
for(row = 0; row <2; row ++)
{
if(row > 2)
{
cout << "Invalid choice" << endl;
}
//This sets the output on the board to X and Os.
for(clmn = 0; clmn <2; clmn++)
{
if(clmn > 2)
{
cout << "Invalid choice" << endl;
}
if(TheBoard[row][clmn]==0)
{
TheBoard[row][clmn]=' ';
}
if (TheBoard[row][clmn]== 1)
{
TheBoard[row][clmn] = 'X';
}
if (TheBoard[row][clmn]== 2)
{
TheBoard[row][clmn] = 'O';
}
}
}
}
//Analyzes the board to determine if there is a winner, and returns X, O, or D (Draw)
char determineWinner()
{
//Checks the rows
for (int i = 0; i < 3; i++)
{
if (TheBoard[i][0] == TheBoard[i][1]
&& TheBoard[i][1] == TheBoard[i][2]
&& TheBoard[i][0] != ' ')
{
return TheBoard[i][0];
}
}
//Checks the Columns
for (int i = 0; i < 3; i++)
{
if (TheBoard[0][i] == TheBoard[1][i]
&& TheBoard[1][i] == TheBoard[2][i]
&& TheBoard[0][i] != ' ')
{
return TheBoard[0][i];
}
}
//Check to see if diagonal moves indicate a winner
if (TheBoard[0][0] == TheBoard[1][1]
&& TheBoard[1][1] == TheBoard[2][2]
&& TheBoard[0][0] != ' ')
{
return TheBoard[0][0];
}
if (TheBoard[2][0] == TheBoard[1][1]
&& TheBoard[1][1] == TheBoard[0][2]
&& TheBoard[2][0] != ' ')
{
return TheBoard[2][0];
}
return 'D';
}
//Beginning of fuction TicTacToe
void PlayOne ()
{
//Declare the following local variables
const int MaxMoves=9;
char TheWinner= ' ';
int NumMoves= 0;
//Utilizing the do while repitition structure
do
{
SwitchPlayer (CurrentPlayer);
//Call the function showboard
ShowBoard ();
//Prompt the user to input their moves and input into the console via CIN
cout<<"\n\n Player "<<CurrentPlayer<<endl;
cout<<"Enter Your Row (0, 1, 2): ";
cin>>row;
cout<<"Enter Your Column (0, 1, 2): ";
cin>>clmn;
//Post the Move To The Board
PostMove (row, clmn, CurrentPlayer);
//Determine if the game has a winner yet
TheWinner=determineWinner () ;
//Keep Track of the Number of Moves
NumMoves++;
}
while ((TheWinner=='D')&&(NumMoves < MaxMoves));
//Shows the Ending Board
ShowBoard ();
if (TheWinner != 'D')
cout<<"\n\nThe Winner is Player: "<<TheWinner;
else
cout<<"\n\nThe Game Was a Draw: "<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
//Call the following functions to run
PlayOne ();
//Explain to user what this program will do, and prompt for first move.
cout<<"This program plays Tic-Tac-Toe, and determines whether the game has concluded."<<endl;
cout<<"Please select a location for your first X."<<endl;
//Instruct the console to remain open
_getch();
return 0;
}
|