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
|
# include <iostream>
# include <stdlib.h> //This is for the "system()" command
using namespace std;
void Printarray();
bool check(); // your function here didn't match the one below and it doesn't need parameters
char array[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};// why not make your array global?
int iplayer = 1; //to keep track of what player is playing, 1=player 1, and 2=player 2
int main ()
{
char player1, player2; // what are you using these for?
int num, turn=0;
//bool win = false, winner; these are unnecessary
char* winner;
while (check() == false) //This should just be the function to simplify
{
cout << endl;
if (turn % 2 == 0){
iplayer = 1;
cout << "Enter the number that corresponds to the spot you wish to pick player 1 ( X )" << '\n'<< '\n';
Printarray();
cout << '\n'<< '\n';
cin >> num;
array[num -1] = 'X';
Printarray();
cout << '\n'<< '\n';
}else {
iplayer = 2;
cout << "Enter the number that corresponds to the spot you wish to pick player 2 ( O )" << '\n'<< '\n';
Printarray();
cout << '\n'<< '\n';
cin >> num;
array[num -1] = 'O';
Printarray();
cout << '\n'<< '\n';
}
turn++;
}
if(iplayer == 1) winner = "Player 1";
else winner = "Player 2";
cout << "The winner is: " << winner << "!!!" << endl;
system("pause");
return 0;
}
bool check() { //the variable 'win' is in main's scope, not here.
//This initailly didn't even check Os because it returned false from the Xs!
//And indentations make your code MUCH EASIER to read.
if(iplayer == 1) {// now it checks which player to check
if ((array[0] == 'X') && (array[3] == 'X') && (array[6] == 'X')){
return true;}
if ((array[1] == 'X') && (array[4] == 'X') && (array[7] == 'X')){
return true;}
if ((array[2] == 'X') && (array[5] == 'X') && (array[8] == 'X')){
return true;}
if ((array[0] == 'X') && (array[1] == 'X') && (array[2] == 'X')){
return true;}
if ((array[3] == 'X') && (array[4] == 'X') && (array[5] == 'X')){
return true;}
if ((array[6] == 'X') && (array[7] == 'X') && (array[8] == 'X')){
return true;}
if ((array[0] == 'X') && (array[4] == 'X') && (array[8] == 'X')){
return true;}
if ((array[2] == 'X') && (array[4] == 'X') && (array[6] == 'X')){
return true;}
else return false;
}else {
if ((array[0] == 'O') && (array[3] == 'O') && (array[6] == 'O')){
return true;}
if ((array[1] == 'O') && (array[4] == 'O') && (array[7] == 'O')){
return true;}
if ((array[2] == 'O') && (array[5] == 'O') && (array[8] == 'O')){
return true;}
if ((array[0] == 'O') && (array[1] == 'O') && (array[2] == 'O')){
return true;}
if ((array[3] == 'O') && (array[4] == 'O') && (array[5] == 'O')){
return true;}
if ((array[6] == 'O') && (array[7] == 'O') && (array[8] == 'O')){
return true;}
if ((array[0] == 'O') && (array[4] == 'O') && (array[8] == 'O')){
return true;}
if ((array[2] == 'O') && (array[4] == 'O') && (array[6] == 'O')){
return true;}
else return false;
}
}
void Printarray(){ // you don't need to pass a parameter to this when the variable being checked is global
for (int j=0; j<=8; j++){
cout<<array[j]<<" ";
if (j ==2 || j == 5) {
cout<<endl;
}
} //You didn't have enough curly brackets in here, indent your code to make it easier to read!
}
|