473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

C++ TIC TAC TOE game help please (OXO GAME)

Hello, so i made this tic tac toe game, its playable between 2 people. but i cant figure out how to make it between me and the computer... any idea how to do that with the use of % operator and srand and rand.... with the ctime library?

THIS IS how far i got, and it doesnt do anything new...

/Include the libraries
#include <iostream>
#include <string>
#include <ctime>
//Using the standard namespace
using namespace std;

// Declare global variables
char Board[9];

// Declare functions
void showBoard ( );
bool moveIsValid (int m);
int whoWon ( ); // Returns 0 if no one has won, 1 of player 1 has won, and 2 if player 2 has won

void main ( )
{
srand (time (NULL) );

//Declare local variable
string Player_1_Name;
int Whose_Turn = 1; // 1 means it's player 1's turn, 2 means it's player 2's turn
int Move; // Stores where the player wants to move
int Total_Moves = 1;
int Computer = rand ( ) % 8 + 1;

//Assing values to the playing board
Board[0] = '0';
Board[1] = '1';
Board[2] = '2';
Board[3] = '3';
Board[4] = '4';
Board[5] = '5';
Board[6] = '6';
Board[7] = '7';
Board[8] = '8';

// Get player names
cout <<"Player 1: Please enter your name." << endl;
cin >> Player_1_Name;



while (whoWon ( ) == 0 && Total_Moves <9)
{

// Do this until the player chooses a valid move
do
{

// Show the board
showBoard ( );

// Tell which player to move
if (Whose_Turn == 1)
{
cout << Player_1_Name << ": It's your turn." << endl;
}
else
{
cout << Computer << ": It's the computer's turn." << endl;
}
// Get the move
cout << " Enter the number of the spot where you'd like to move." << endl;
cin >> Move;
} while (moveIsValid (Move) != true);

//Add 1 to Total_Moves
Total_Moves++;

// Change whose turn it is
switch (Whose_Turn)
{
case (1):
{
Board[Move] = 'x';
Whose_Turn = 2;
break;
}
case (2):
{
Board[Move] = 'o';
Whose_Turn = 1;
}
}
}
// Show the board
showBoard ( );

if (whoWon ( ) == 1)
{
cout << Player_1_Name << " has won the game!" << endl;
}
else if (whoWon ( ) == 2)
{
cout << Computer << "has won the game!" << endl;
}
else
{
cout << " It's a tie game!" << endl;
}

system ("PAUSE");
}
void showBoard ( )
{
cout << endl;
cout << Board[0] << " | " << Board[1] << " | " << Board[2] << endl;
cout <<"--+---+--" << endl;
cout << Board[3] << " | " << Board[4] << " | " << Board[5] << endl;
cout <<"--+---+--" << endl;
cout << Board[6] << " | " << Board[7] << " | " << Board[8] << endl;
cout << endl;

}

bool moveIsValid (int m )
{
if(Board[m] !='x' && Board[m] != 'o')
{
return true;
}
else
{
return false;
}
}

int whoWon ( )
{
if (Board[0] == Board[1] && Board[1] == Board[2])
{
if(Board[0] == 'x')
{
return 1;
}
else
{
return 2;
}

}
if (Board[3] == Board[4]&& Board[4] == Board[5])
{
if(Board[3] == 'x')
{
return 1;
}
else
{
return 2;
}

}
if (Board[6] == Board[7] && Board[7] == Board[8])
{
if(Board[6] == 'x')
{
return 1;
}
else
{
return 2;
}

}
if (Board[0] == Board[3] && Board[3] == Board[6])
{
if(Board[0] == 'x')
{
return 1;
}
else
{
return 2;
}

}
if (Board[1] == Board[4] && Board[4] == Board[7])
{
if(Board[1] == 'x')
{
return 1;
}
else
{
return 2;
}

}
if (Board[2] == Board[5] && Board[5] == Board[8])
{
if(Board[2] == 'x')
{
return 1;
}
else
{
return 2;
}

}
if (Board[0] == Board[4] && Board[4] == Board[8])
{
if(Board[0] == 'x')
{
return 1;
}
else
{
return 2;
}

}
if (Board[2] == Board[4] && Board[4] == Board[6])
{
if(Board[2] == 'x')
{
return 1;
}
else
{
return 2;
}

}
return 0;
}
Nov 8 '11 #1
1 3316
weaknessforcats
9,208 Expert Mod 8TB
Go back to your game using 2 people that works.

Just replace the 2nd player keyboard inout with a call to a function that you will write that makes the move.

I would remove the code for th 2nd player and put it inside this function that will now be called from main(). Build the game and verify that i still works. You are now the computer.

Now replace the contents of the function with code to produce a real computer move. As a first cut have the comuter move to any square that is available. Build and verify the game still works.

Now augment the move so that the computer blocks where the other user has two squares. Buid and verify the game still works.

Note that by doing this incrementally and testing each time, your game becomes increasingly effective and at each step it always works. This is a lot diffrent from writing a ton of code and then doing a giant debug only to find out most of your code needs to be rewritten.
Nov 9 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: RootShell | last post by:
Anyone knows where i can find a FREE PHP GAME script which is based on the genre "Name The Game" where a picture of a game is shown and the user has to guess which game it is by a mean of 5...
4
by: Daedric | last post by:
Hello and thanks in advance to anyone who offers help. To make this simple, let's say I have a game which has 100 different monsters. I want a binary data file to hold all of these. It would...
1
by: Brian Basquille | last post by:
Hello all. Have been working on the Air Hockey game on and off for a couple of weeks now.. but have had plenty of other assignments to keep me busy along with it. But i would like some...
7
by: Gasten | last post by:
Hello. The last weeks I've been coding a roguelike (you know, like nethack) in python using the nCurses library. Some week ago I ran into a problem: When I made the object for messagebar-output, I...
5
by: Kraken | last post by:
Hi, i have a bit of a problem here. I have an assignment to do an animal guessing game using an original database and updating it as the user enters new animals in it. The program enters the file...
0
by: indhu | last post by:
Hi all, I have doubt regarding this. How to Convert c++ game to Flash game. what r the advantages and disadvantages. can anyone help me in this. thanks in advance
6
raubana
by: raubana | last post by:
I wanna make a game called planetiod where you create planets and try not to blow them up, but i'm having a hard time with it. If you could, please tell me if you could help me with some modules or...
0
by: Advertiser for `2D Games Development Central` | last post by:
New to game development? Need a headstart in creating that first game of yours? Want to meet others who share a passion for playing and creating games? Need support, but don't know where go for it?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.