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
|
/*Level:
Beginner
Your ‘Assignment’ Stage 1:
Write a simple program that will allow two ‘human’ players to play Tic-tac-toe on the screen, there is no need to determine Win, lose, or draw yet.
Just make sure that the players can not put marks in places that already have marks and finish the ‘playing’ sequence when all spaces are taken.
Stage 2:
Modify your program from stage 1 to allow your program to determine Win, lose, or draw states.
Stage 3:
Modify your program from stage 2, to have the option of playing against a ‘computer’ opponent.
*/
/*1) Display the Grid
2) Display the 'pieces' on the grid
3) Determine if more pieces can be placed
4) Enter position for next piece.
For stages 2 point (3) can be extended to check for Win, Lose or Draw, and for stage 3 you can develop a (4a) wich is an computer placement.
Having done a first take on simplifying the problem, you then think about relationships between the stages - common data, functions, classes, etc.*/
#include <iostream>
#include <string>
using std::cout;
using std::cin;
void DispGrid();
int Move1();
int Move2();
bool CheckMove(int);
bool GameWin=false;
char one=1, two=2, three=3, four=4, five=5, six=6, seven=7, eight=8, nine=9;
void RedrawGrid(int,char);
//============================================================================================
int main(int argc, char *argv[])
{
cout<<"Player one = X\nPlayer two = O\n";
while(GameWin!=true){ //playing loop
DispGrid();
Move1();
DispGrid();
Move2();
}
cin.clear();
cin.ignore();
cout<<"Press enter to continue...";
cin.get();
return 0;
}
//===========================================================================================================
void DispGrid() //display the grid function
{
cout<<"\n"<<one<<"|"<<two<<"|"<<three<<"\n";
cout<<"-+-+-\n";
cout<<four<<"|"<<five<<"|"<<six<<"\n";
cout<<"-+-+-\n";
cout<<seven<<"|"<<eight<<"|"<<nine<<"\n\n";
}
//==================================================================================================
bool CheckMove(int move, bool check) //function to check if the move is valid
{
check=true;
switch(move)
{
case 1: //if the spot is occupied this returns false, otherwise it returns true
if(one=='X'||one=='O')
{cout<<"That's an invalid move, please try again!\n";
check=false;}
break;
case 2:
if(two=='X'||two=='O')
{cout<<"That's an invalid move, please try again!\n";
check=false;}
break;
case 3:
if(three=='X'||three=='O')
{cout<<"That's an invalid move, please try again!\n";
check=false;}
break;
case 4:
if(four=='X'||four=='O')
{cout<<"That's an invalid move, please try again!\n";
check=false;}
break;
case 5:
if(five=='X'||five=='O')
{cout<<"That's an invalid move, please try again!\n";
check=false;}
break;
case 6:
if(six=='X'||six=='O')
{cout<<"That's an invalid move, please try again!\n";
check=false;}
break;
case 7:
if(seven=='X'||seven=='O')
{cout<<"That's an invalid move, please try again!\n";
check=false;}
break;
case 8:
if(eight=='X'||eight=='O')
{cout<<"That's an invalid move, please try again!\n";
check=false;}
break;
case 9:
if(nine=='X'||nine=='O')
{cout<<"That's an invalid move, please try again!\n";
check=false;}
break;
}
}
//==================================================================================================
int Move1() //player number 1's move
{
int place;
bool check=true;
start:
cout<<"Where would you like to place your X? ";
cin>>place;
CheckMove(place,check);
if(check=false)
goto start;
char sg='X';
RedrawGrid(place,sg);
}
//==================================================================================================
int Move2() //player number 2's move
{
int place;
bool check=true;
start:
cout<<"Where would you like to place your O? ";
cin>>place;
CheckMove(place,check); //check if the move is valid
if(check=false)
goto start; //get the move again if it's not valid
char sg='O';
RedrawGrid(place,sg);
}
void RedrawGrid(int place, char sg) //function to change the grid accordingly to the users input
{
if(place=1)
one=sg;
else if(place=2)
two=sg;
else if(place=3)
three=sg;
else if(place=4)
four=sg;
else if(place=5)
five=sg;
else if(place=6)
six=sg;
else if(place=7)
seven=sg;
else if(place=8)
eight=sg;
else if(place=9)
nine=sg;
}
|