![]() |
| | #1 |
| Registered User Join Date: Nov 2005
Posts: 25
| function trouble Trying to create a tictactoe game, and am working on a function for getting one of the two users playing to see what the outcome of the game is. So far I have got it as follows: Code: TicTacToe:: Status TicTacToe:: gameStatus gameStatus(void)
{
// Check for win in diagonals //
if (board [0][0]!= '_' && board [0][0] == board [1][1]
&& board [0][0] == board [2][2])
return WIN;
else if (board [0][2]!= '_' && board [0][2] == board [1][1]
&& board [0][2] == board [2][0])
return WIN;
//** End Check for win in diagonals ** //
// Check for win in rows //
if (board [0][0]!= '_' && board [0][0] == board [1][0]
&& board [0][0] == board [2][0])
return WIN;
else if (board [0][1]!= '_' && board [0][1] == board [1][1]
&& board [0][1] == board [2][2])
return WIN;
else if (board [0][2]!= '_' && board [0][2] == board [1][2]
&& board [0][2] == board [2][2])
return WIN;
//** End Check for win in rows **//
// Check for win in columns //
if (board [0][0]!= '_' && board [0][0] == board [0][1]
&& board [0][0] == board [0][2])
return WIN;
else if (board [1][0]!= '_' && board [1][0] == board [1][1]
&& board [1][0] == board [1][2])
return WIN;
else if (board [2][0]!= '_' && board [2][0] == board [2][1]
&& board [2][0] == board [2][2])
return WIN;
// ** End Check for win in columns ** //
// Check for a completed game //
return CONTINUE; // game is not finished
// ** End Check for completed game ** //
// Check for a draw //
return DRAW; // game is a draw
// ** End Check for draw ** //
}
What I need to do now is check for a drawn game, which I think I need to state something like, if move >9 (it’s a 3x3 board) then Return Draw. And also check for a completed game? But I’m stuck on how to do these two! Can someone please advise if I am going right so far and also how I’d go about checking for a completed game and a draw? Many Thanks Rebel |
| rebel is offline | |
| | #2 |
| Registered User Join Date: Nov 2005
Posts: 52
| Seems like your thoughts are on the right track. The algorithm that makes sense to me is this: 1. Player moves 2. Check for a win 3. Check for a draw 4. If no win or draw, next move In other words, if you've already checked for a win and a draw, you KNOW the answer to if the game is complete already. Oh, and as a stylistic matter, a common way of handling your return value is to set it a variable to the default (CONTINUE in your case) at the top of the function, then modify it as needed in your conditionals, then at the bottom of the function, just return the var you defined at the top. This makes it MUCH easier to understand and follow the function IMO. Last edited by Stuka; 12-15-2005 at 09:02 AM. |
| Stuka is offline | |
| | #3 |
| Registered User Join Date: Mar 2005
Posts: 140
| to check if the game is complete If any array value is still it's initial value then it's not complete. Code: int isComplete(array)
{
for(i=0; i < row; i++) {
for(j=0; j < col; j++) {
if(array[i][j] == empty) {
return 0;
}
}
}
return 1;
}
for a draw if game is complete and player one hasn't won and player two hasn't won then the game is a draw |
| spydoor is offline | |
| | #4 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| Code: int isComplete(array) Code: int isComplete(int array[][size])
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort |
| dwks is offline | |
| | #5 |
| Registered User Join Date: Nov 2005
Posts: 25
| Hi Guys, Thanks for your help - much appreciated! Think i'm getting somewhere now, but am a little stuck. Here's what I have done so far: Code: // TICTACTOE.H
// A Program to run the Tic-Tac-Toe Program
#include <iostream.h>
#include <iomanip.h>
class TicTacToe
{
private:
enu Status {WIN, DRAW, CONTINUE };
char board [3] [3];
public:
TicTacToe ();
void makeMove ();
void printBoard ();
bool validMove (int r, int c);
bool xoMove (int input)
Status gameStatus ();
void holdscreen ();
};
TicTacToe:: TicTacToe ()
{
// Initialising TicTacToe //
for (int r = 0; r < r; r++) //looping rows?
for (int c = 0; c < c; c++) //looping columns?
validMove[r][c] = '_';
// ** End TicTacToe ** //
// ** IS THIS SETTING EVERY ELEMENT ON THE BOARD TO A SPACE? ** //
}
bool TicTacToe::validMove (int r, int c)
{
// Validate Move Start for X //
cout << "Enter coordinates for X: ";
cin >> r;
while(true) {
if ((r>=3) || (r<0))
cout<<endl<<"invalid move. try again with a number from 0 to 2: ";
else break;}
// ** End Validate Move Start for X ** //
// Validate Move Start for O //
cout << "Enter coordinates for O: ";
cin >> c;
while(true) {
if ((c>=3) || (c<0))
cout<<endl<<"invalid move. try again with a number from 0 to 2: ";
else break;}
// ** End Validate Move Start for O ** //
}
TicTacToe:: Status TicTacToe:: gameStatus gameStatus(void)
{
int a;
// Check for win in diagonals //
if (board [0][0]!= '_' && board [0][0] == board [1][1]
&& board [0][0] == board [2][2])
return WIN;
else if (board [0][2]!= '_' && board [0][2] == board [1][1]
&& board [0][2] == board [2][0])
return WIN;
//** End Check for win in diagonals ** //
// Check for win in rows
if (board [0][0]!= '_' && board [0][0] == board [1][0]
&& board [0][0] == board [2][0])
return WIN;
else if (board [0][1]!= '_' && board [0][1] == board [1][1]
&& board [0][1] == board [2][2])
return WIN;
else if (board [0][2]!= '_' && board [0][2] == board [1][2]
&& board [0][2] == board [2][2])
return WIN;
//** End Check for win in rows **//
// Check for win in columns
if (board [0][0]!= '_' && board [0][0] == board [0][1]
&& board [0][0] == board [0][2])
return WIN;
else if (board [1][0]!= '_' && board [1][0] == board [1][1]
&& board [1][0] == board [1][2])
return WIN;
else if (board [2][0]!= '_' && board [2][0] == board [2][1]
&& board [2][0] == board [2][2])
return WIN;
// ** End Check for win in columns ** //
// Check for a completed game
{
for(r=0; r < r; r++) {
for(c=0; c < c; C++) {
if(array[r][c] == empty) {
return 0;
}
}
}
return CONTINUE;
}
else
return DRAW; // game is a draw
}
void TicTacToe::printBoard (void)
{
cout << " 0 1 2\n\n";
for ( int r=0; r < 3; ++r ) {
cout << r;
for ( int c=0; c < 3; ++c ) {
cout << setw (3) << static_cast < char > ( board [r] [c] );
if (c !=2 )
cout << " |";
}
if (r != 2)
cout << "\n ______|______|______"
<< "\n ______|______|______\n"
}
cout <<"\n\n";
void TicTacToe::makeMove(void)
{
printBoard();
while ( true ) {
if (xoMove ( 'X' ) )
break;
else if (xoMove ( 'O' ) )
break;
}
}
bool TicTacToe::xoMove (int symbol )
{
int x,y;
do {
cout << "Player" << static_cast < char > (symbol) << " enter move: ";
cin >> x >> y;
cout << '\n';
} while ( !validMove ( x, y ) );
board [x] [y] = symbol;
printBoard();
Status xoStatus = gamestatus();
if ( xoStatus == WIN ) {
cout << "Player " << static_cast < char > (symbol ) << " wins!\n";
return true;
}
else if (xoStatus == DRAW ) {
cout << "Game is a draw. \n";
return true;
}
else //Continue
return false;
}
initialising the table Code: TicTacToe:: TicTacToe ()
{
// Initialising TicTacToe //
for (int r = 0; r < r; r++) //looping rows?
for (int c = 0; c < c; c++) //looping columns?
validMove[r][c] = '_';
// ** End TicTacToe ** //
// ** IS THIS SETTING EVERY ELEMENT ON THE BOARD TO A SPACE? ** //
}
Checking the game status Code: TicTacToe:: Status TicTacToe:: gameStatus gameStatus(void)
{
int a;
// Check for win in diagonals //
if (board [0][0]!= '_' && board [0][0] == board [1][1]
&& board [0][0] == board [2][2])
return WIN;
else if (board [0][2]!= '_' && board [0][2] == board [1][1]
&& board [0][2] == board [2][0])
return WIN;
//** End Check for win in diagonals ** //
// Check for win in rows
if (board [0][0]!= '_' && board [0][0] == board [1][0]
&& board [0][0] == board [2][0])
return WIN;
else if (board [0][1]!= '_' && board [0][1] == board [1][1]
&& board [0][1] == board [2][2])
return WIN;
else if (board [0][2]!= '_' && board [0][2] == board [1][2]
&& board [0][2] == board [2][2])
return WIN;
//** End Check for win in rows **//
// Check for win in columns
if (board [0][0]!= '_' && board [0][0] == board [0][1]
&& board [0][0] == board [0][2])
return WIN;
else if (board [1][0]!= '_' && board [1][0] == board [1][1]
&& board [1][0] == board [1][2])
return WIN;
else if (board [2][0]!= '_' && board [2][0] == board [2][1]
&& board [2][0] == board [2][2])
return WIN;
// ** End Check for win in columns ** //
// Check for a completed game
{
for(r=0; r < r; r++) {
for(c=0; c < c; C++) {
if(array[r][c] == empty) {
return 0;
}
}
}
return CONTINUE;
}
else
return DRAW; // game is a draw
}
Thanks Reb |
| rebel is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Calling a Thread with a Function Pointer. | ScrollMaster | Windows Programming | 6 | 06-10-2006 08:56 AM |
| Having Trouble Passing typedef Arrays to a Function | jlharrison | C Programming | 1 | 03-27-2006 12:06 PM |
| Including lib in a lib | bibiteinfo | C++ Programming | 0 | 02-07-2006 02:28 PM |
| Dikumud | maxorator | C++ Programming | 1 | 10-01-2005 06:39 AM |
| Setting int pointers to one function (not using five functions!) | Marc Sharp | C Programming | 4 | 11-23-2003 07:15 AM |