Explain some code to me?
Mar 19, 2012 at 12:22am UTC
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
#include <iostream>
using namespace std;
char board[9] = { '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' };
void drawBoard();
bool checkWinner();
void movePlayer(bool );
void moveComputer(bool );
bool possibleMove(int );
int main()
{
bool player = false ; /* true = X , false = O */
int i= 0;
drawBoard();
//im confused as to what is happening in this while loop.
while (!checkWinner())
if ( i==0 || i==2 || i==4 || i==6 || i==8 )
{
i++;
{
if (player == true )
player = false ;
else
player = true ;
movePlayer(player);
}
}
else if ( i==1 || i == 3 || i==5 || i==7 )
{
i++;
{
if (player == true )
player = false ;
else
player = true ;
moveComputer(player);
}
}
else
cout << " It's a TIE !!! " << endl;
cin.get();
if (player == true )
cout << "Player one WINS !!!" << endl;
else
cout << "The computer WINS !!!" << endl;
}
void drawBoard()
{
cout << "\n " << board[0] << " | " << board[1] << " | " << board[2] << endl
<< " ---------" << endl
<< " " << board[3] << " | " << board[4] << " | " << board[5] << endl
<< " ---------" << endl
<< " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
}
bool checkWinner()
{
if (board[0] == board[1] && board[2] == board[0] )
return true ;
else if (board[3] == board[4] && board[5] == board[3])
return true ;
else if (board[6] == board[7] && board[8] == board[6])
return true ;
else if (board[0] == board[3] && board[6] == board[0])
return true ;
else if (board[1] == board[4] && board[7] == board[1])
return true ;
else if (board[2] == board[5] && board[8] == board[2])
return true ;
else if (board[0] == board[4] && board[8] == board[0])
return true ;
else if (board[2] == board[4] && board[6] == board[2])
return true ;
else
return false ;
}
void movePlayer(bool whichPlayer)
{
//im also confused as to what is going on here
int place;
cout << "\n Player one, make your move: " ;
cin >> place;
if (possibleMove(place))
{
if (whichPlayer == true )
board[place-1] = 'X' ;
else
board[place-1] = 'O' ;
}
else
movePlayer(whichPlayer);
drawBoard();
}
void moveComputer(bool whichPlayer)
{
int place;
cout << "\n Computer, make your move: " ;
cin >> place;
if (possibleMove(place))
{
if (whichPlayer == true )
board[place-1] = 'X' ;
else
board[place-1] = 'O' ;
}
else
moveComputer(whichPlayer);
drawBoard();
}
bool possibleMove(int place)
{
if (board[place-1] == 'X' || board[place-1] == 'O' )
return false ;
else
return true ;
}
i also want to add in a random number generator that i wrote last week to choose the computers move rather then a 2nd player, any help with the comments in the code or with adding in a random number generator.
Mar 19, 2012 at 12:25am UTC
Last edited on Mar 19, 2012 at 12:29am UTC
Mar 19, 2012 at 12:28am UTC
Omar Alamy wrote:Explain some code to me?
The code you posted, I presume? Nobody will do that. If you write code, you're expected to know what you're writing. Did you write the code?
Wazzak
Topic archived. No new replies allowed.