|
 |
|
 |
01-23-2005, 09:13 AM
|
#1 (permalink)
|
|
Code Monkey
Join Date: Oct 2004
Posts: 57
|
Function for Tic Tac Toe
Hey everyone...
How can I make a function for two different characters in a char variable?
Code:
if( ( letters[7] == 'O' && letters[7] == letters[5] && letters[5] == letters[3] ) // diagonal - t.l. --> b.r.
|| ( letters[1] == 'O' && letters[1] == letters[5] && letters[5] == letters[9] ) // diagonal - b.l. --> t.r.
|| ( letters[7] == 'O' && letters[7] == letters[4] && letters[4] == letters[1] ) // vertical - left
|| ( letters[8] == 'O' && letters[8] == letters[5] && letters[5] == letters[2] ) // vertical - middle
|| ( letters[9] == 'O' && letters[9] == letters[6] && letters[6] == letters[3] ) // vertical - right
|| ( letters[7] == 'O' && letters[7] == letters[8] && letters[8] == letters[9] ) // horizontal - top
|| ( letters[4] == 'O' && letters[4] == letters[5] && letters[5] == letters[6] ) // horizontal - middle
|| ( letters[1] == 'O' && letters[1] == letters[2] && letters[2] == letters[3] ) ) // horizontal - bottom
{
cout << endl << "O is the WINNER!!" << endl; // says winner
playboard(); // displays end playboard
break; // ends loop
}
I have to make it so that when I call the function for a certain character, it replaces all the 'O's with the character...
A little confused...
|
|
|
01-23-2005, 10:34 AM
|
#2 (permalink)
|
|
Code Monkey
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
|
PHP Code:
bool checkBoard (char charToCheck, char* letters) {
if (letters[7] == charToCheck /* && blah blah blah */) {
cout << charToCheck << " is the winner!\n";
return true;
}
return false;
}
/* In your main function */
while (...) {
if (checkBoard ('o', letters)) break;
}
Hope this helps!
-Ted
__________________
while(1) fork();
|
|
|
01-23-2005, 01:05 PM
|
#3 (permalink)
|
|
Code Monkey
Join Date: Oct 2004
Posts: 57
|
Yay! It works! Thanks a bunch! 
|
|
|
01-23-2005, 03:26 PM
|
#4 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Say, how about some planning before coding  .
My presence here is all about learning young folks how to think before coding. In fact, coding becomes super easy if one manages to analyse a bit first.
Let me tell you how I did half a tic-tac-toe (see code below) program in 15 minutes!
First I want to know about the domain problem. Or in normal words:
what the heck are we talking about?
Then I want to make sure what's going on with this application. To find out I play a few games to get the "global" idea. From there I know the main functions. Let us call this use case analisys.
Then I'm thinking like this:
Quote:
|
Hey, the playground is basically a 3x3 matrix and the elements in that matrix can be owned by Player_1, Player_2, or it can be empty.
|
So I come up with this:
Code:
enum Owner
{
PLAYER_1,
PLAYER_2,
EMPTY
};
And now I need to make the play ground by creating that 3x3 matrix and encapsulate it with typical things that can occur on that playground. For example, creating the playground. Just look at the class, and things get obvious. This is the result of use case analisys (don't let that difficult phrase scare you!).
Code:
class Field
{
public:
Field();
Owner winner() const;
bool full() const;
void set_field(Position, Owner);
private:
static bool equal(Owner, Owner, Owner);
Owner _owner[3][3];
};
But each field in the matrix has a position (co-ordinate). I'll make a seperate struct (or class if you will) for performance reasons. Just a demonstration of a nice technique. You could integrate it into the Field class if you will.
Code:
struct Position
{
int _x, _y;
Position(int x, int y) :
_x(x), _y(y)
{}
Position() :
_x(-1), _y(-1)
{}
};
So now I have my domain, only because I played a few games and thought for 5 minutes using nothing else but a pencil and one page! The actual implementation takes only a second. Observe the complete code below. Would you believe that I spent more time typing this on the forum then developing the core of the game?
Here we go:
Code:
struct Position
{
int _x, _y;
Position(int x, int y) :
_x(x), _y(y)
{}
Position() :
_x(-1), _y(-1)
{}
};
//--------------------------------------------------
enum Owner
{
PLAYER_1,
PLAYER_2,
EMPTY
};
//--------------------------------------------------
class Field
{
public:
Field();
Owner winner() const;
bool full() const;
void set_field(Position, Owner);
private:
static bool equal(Owner, Owner, Owner);
Owner _owner[3][3];
};
//--------------------------------------------------
Field::Field()
{
for(unsigned i=0; i<3; ++i)
{
for(unsigned j=0; j<3; ++j)
{
_owner[i][j] = EMPTY;
}
}
}
//--------------------------------------------------
void Field::set_field(Position pos, Owner owner)
{
_owner[pos._x][pos._y] = owner;
}
//--------------------------------------------------
bool Field::equal(Owner a, Owner b, Owner c)
{
if(a == b && b == c)
{
return a != EMPTY;
}
return false;
}
bool Field::full() const
{
for(unsigned i=0; i<3; ++i)
{
for(unsigned j=0; j<3; ++j)
{
if( _owner[i][j] == EMPTY)
{
return false;
}
}
}
}
//--------------------------------------------------
Owner Field::winner() const
{
for(int i=0; i<3; ++i)
{
if(equal(_owner[i][0], _owner[i][1], _owner[i][2]))
return _owner[i][0];
}
for(int j=0; j<3; ++j)
{
if(equal(_owner[0][j], _owner[1][j], _owner[2][j]))
return _owner[0][j];
}
if(equal(_owner[0][0], _owner[1][1], _owner[2][2]))
return _owner[0][0];
if(equal(_owner[2][0], _owner[1][1], _owner[0][2]))
return _owner[0][2];
return EMPTY;
}
//--------------------------------------------------
int main(int argc, char *argv[])
{
//Create the play ground.
Field theField;
//Create the players here.
//...magic code here...
while(1)
{
// 1) Determine which position player_1 or player_2 chooses.
// 2) Store it in Field::_owner.
// 3) Update the visualisation (GUI, console, bash, iMac, etc.).
// 4) Do we have a winner perhaps?
if(theField.winner() != EMPTY)
{
//We have a winner.
break;
}
// 5) Otherwise, do we hava draw (no winner, but all fields full).
if(theField.full() )
{
//All fields are used. It must be a draw if we manage to get here.
break;
}
// 6) No winner and some field(s) is/are empty.
// ...magic code to switch players turn...
}
// 7) We have a winner or it is draw.
// ...magic code to visualise and wrap things (application, cleanup) up...
return 0;
}
Yup. That's it.
See if you like it.
__________________
|
|
|
01-23-2005, 05:22 PM
|
#5 (permalink)
|
|
Code Monkey
Join Date: Oct 2004
Posts: 57
|
Cool, Valmont!
Our assignment was to have a human play against the computer and have it so that either the computer wins all the time or there is a tie... Unfortunately, the only way I was able to make this program was using if statement to find every possibility so that the computer can make the right moves... It is coming along quite nicely at 777 lines! 
|
|
|
01-24-2005, 11:30 AM
|
#6 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Post it when you're done if you will. Let's take a peek at it together.
__________________
|
|
|
01-28-2005, 07:36 PM
|
#7 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
well since he's not posting his code, here's mine...
Code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int losecount(0), tiecount(0), valid(0), lastmove(0);
string name;
char toe[9]; //// name of char array which places X's and O's
void resetboard(); //// resets the board
void human(); //// function which makes toe[?] = X
void drawboard(); //// Draws the Board
int firstmove(); //// First computer move
int secondmove(); //// Second computer move
int thirdmove(); //// third computer move
void fourthmove(); //// fourth computer move
void automove(); //// autofills the last space with X
int valcheck(int valid); //// checks to make sure that the number enterd by the human is valid
int wincheck(); //// checks all possible combinations for a win
////////////////////////////////////////////////////////////////////////////MAIN//////////////////////////////////////////////////////////////////
int main()
{
int lose(0); //// this int is used to end the game (inner loop) if a loss occurs
char yesno('y'); //// this ends the program (outer loop) if they pic 'n'
cout << "TIC-TAC-TOE" << endl << endl << "You are 'O'. Use the number pad to place your O's." << endl << "Don't forget to make sure that num-lock is on." << endl << "Please enter your name: ";
cin >> name;
cout << "When you see - " << name << ": - that means it is your turn to move." << endl << "Good Luck!" << endl << endl;
while (yesno == 'y' || yesno == 'Y') // outer loop
{
lose = 0;
while (lose == 0) // inner loop
{
resetboard();
int i(1); // this variable is used for human moves
drawboard();
human();
drawboard();
lastmove = firstmove();
drawboard();
while (i == 1)
{
human();
if (valid == 1)
{
i++;
drawboard();
}
else if (valid == 0)
{
cout << "You can't play there." << endl;
}
}
lastmove = secondmove(); // lastmove is used to help keep things efficient by making the computer only check certain situations instead of each possible situation
drawboard();
while (i == 2)
{
human();
if (valid == 1)
{
i++;
drawboard();
}
else if (valid == 0)
{
cout << "You can't play there." << endl;
}
}
lastmove = thirdmove(); // lastmove is used to help keep things efficient by making the computer only check certain situations instead of each possible situation
drawboard();
lose = wincheck(); // if lose = 1 it skips the rest of the inner loop and asks if they want to play again
if (lose == 1)
{
break;
}
while (i == 3)
{
human();
if (valid == 1)
{
i++;
drawboard();
}
else if (valid == 0)
{
cout << "You can't play there." << endl;
}
}
fourthmove();
drawboard();
lose = wincheck();
if (lose == 1)
{
break;
}
automove();
drawboard();
if (lose == 0)
{
cout << "You tied" << endl;
tiecount++;
break;
}
}
cout << "Would you like to play again? (y or n) \t";
cin >> yesno;
if (yesno == 'y' || yesno == 'Y')
{
system("CLS");
}
}
cout << endl << endl << endl << "You lost " << losecount << " times." << endl << "You tied " << tiecount << " times." << endl << "You won 0 times." << endl;
system("PAUSE");
return 0;
}
////////////////////////////////////////////////////////////////////////////Board Reset//////////////////////////////////////////////////////////////////
void resetboard() /// this makes every spot on the board = to a space
{
toe[1] = ' ';
toe[2] = ' ';
toe[3] = ' ';
toe[4] = ' ';
toe[5] = ' ';
toe[6] = ' ';
toe[7] = ' ';
toe[8] = ' ';
toe[9] = ' ';
}
////////////////////////////////////////////////////////////////////////////HUMAN INPUT//////////////////////////////////////////////////////////////////
void human()
{
int num;
cout << name << ": ";
cin >> num;
valid = num;
valid = valcheck(valid);
if (num == 1 && valid == 1) // <<<<< the valid result in integrated in the if statements
{
toe[1]= 'X';
}
if (num == 2 && valid == 1)
{
toe[2]= 'X';
}
if (num == 3 && valid == 1)
{
toe[3]= 'X';
}
if (num == 4 && valid == 1)
{
toe[4]= 'X';
}
if (num == 5 && valid ==1)
{
toe[5]= 'X';
}
if (num == 6 && valid == 1)
{
toe[6]= 'X';
}
if (num == 7 && valid ==1)
{
toe[7]= 'X';
}
if (num == 8 && valid == 1)
{
toe[8]= 'X';
}
if (num == 9 && valid == 1)
{
toe[9] = 'X';
}
}
////////////////////////////////////////////////////////////////////////////DRAW BOARD//////////////////////////////////////////////////////////////////
void drawboard()
{
cout << endl << "\t\t\t " << toe[7] << " | " << toe[8] << " | " << toe[9] << endl << "\t\t\t---|---|---" << endl << "\t\t\t " << toe[4] << " | " << toe[5] << " | " << toe[6] << endl << "\t\t\t---|---|---" << endl << "\t\t\t " << toe[1] << " | " << toe[2] << " | " << toe[3] << endl << endl << endl << endl;
}
////////////////////////////////////////////////////////////////////////////VALIDATOR//////////////////////////////////////////////////////////////////
int valcheck(int valid)
{
if (valid == 1)
{
if (toe[1] == ' ') /// because all possible moves were set to spaces, this enables me to use an if statement say "if toe[?] == to a blank spot...." instead of "if toe[?] != to X or O
{
return 1;
}
}
if (valid == 2)
{
if (toe[2] == ' ')
{
return 1;
}
}
if (valid == 3)
{
if (toe[3] == ' ')
{
return 1;
}
}
if (valid == 4)
{
if (toe[4] == ' ')
{
return 1;
}
}
if (valid == 5)
{
if (toe[5] == ' ')
{
return 1;
}
}
if (valid == 6)
{
if (toe[6] == ' ')
{
return 1;
}
}
if (valid == 7)
{
if (toe[7] == ' ')
{
return 1;
}
}
if (valid == 8)
{
if (toe[8] == ' ')
{
return 1;
}
}
if (valid == 9)
{
if (toe[9] == ' ')
{
return 1;
}
}
return 0;
}
////////////////////////////////////////////////////////////////////////////FIRST MOVE//////////////////////////////////////////////////////////////////
int firstmove()
{
if (toe[1] == 'X' || toe[3] == 'X' || toe[7] == 'X' || toe[9] == 'X')
{
toe[5] = 'O'; cout << "Computer: 5" << endl;
return 1; // this number is what "lastmove" becomes = to. this keeps the next computer move from checking situations which do not apply to this move.
}
if (toe[8] == 'X' || toe[4] == 'X' || toe[6] == 'X' || toe[2] == 'X')
{
toe[5] = 'O'; cout << "Computer: 5" << endl;
return 2;
}
if (toe[5] == 'X')
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
return 3;
}
}
////////////////////////////////////////////////////////////////////////////SECOND MOVE//////////////////////////////////////////////////////////////////
int secondmove()
{
if (lastmove == 1) /// this is the if statement which goes along with the one above this. since one was returned, it only checks this if statements instead of every possible situation.
{
if (toe[7] == 'X' && toe[4] == 'X' || toe[3] == 'X' && toe[2] == 'X')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
return 1;
}
if (toe[1] == 'X' && toe[3] == 'X' || toe[1] == 'X' && toe[6] == 'X' || toe[1] == 'X' && toe[9] == 'X' || toe[3] == 'X' && toe[7] == 'X' || toe[3] == 'X' && toe[4] == 'X')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
return 2;
}
if (toe[1] == 'X' && toe[2] == 'X' || toe[9] == 'X' && toe[6] == 'X')
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
return 3;
}
if (toe[1] == 'X' && toe[7] == 'X' || toe[1] == 'X' && toe[8] == 'X' || toe[7] == 'X' && toe[2] == 'X')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
return 4;
}
if (toe[3] == 'X' && toe[9] == 'X' || toe[3] == 'X' && toe[8] == 'X' || toe[9] == 'X' && toe[2] == 'X')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
return 5;
}
if (toe[1] == 'X' && toe [4] == 'X' || toe[9] == 'X' && toe[8] == 'X')
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
return 6;
}
if (toe[9] == 'X' && toe[7] == 'X' || toe[9] == 'X' && toe[4] == 'X' || toe[7] == 'X' && toe[6] == 'X')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
return 7;
}
if (toe[3] == 'X' && toe[6] == 'X' || toe[7] == 'X' && toe[8] == 'X')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
return 8;
}
}
if (lastmove == 2)
{
if (toe[2] == 'X' && toe[3] == 'X' || toe[2] == 'X' && toe[4] == 'X' || toe[4] == 'X' && toe[7] == 'X')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
return 9;
}
if (toe[4] == 'X' && toe[3] == 'X' || toe[6] == 'X' && toe[1] == 'X')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
return 10;
}
if (toe[2] == 'X' && toe[6] == 'X' || toe[6] == 'X' && toe[9] == 'X' || toe[2] == 'X' && toe[1] == 'X')
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
return 11;
}
if (toe[2] == 'X' && toe[7] == 'X' || toe[8] == 'X' && toe[1] == 'X')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
return 12;
}
if (toe[2] == 'X' && toe[9] == 'X' || toe[8] == 'X' && toe[3] == 'X' || toe[2] == 'X' && toe[8] == 'X')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
return 13;
}
if (toe[4] == 'X' && toe[8] == 'X' || toe[8] == 'X' && toe[9] == 'X' || toe[4] == 'X' && toe[1] == 'X')
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
return 14;
}
if (toe[4] == 'X' && toe[9] == 'X' || toe[6] == 'X' && toe[7] == 'X' || toe[4] == 'X' && toe[6] == 'X')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
return 15;
}
if (toe[6] == 'X' && toe[8] == 'X' || toe[8] == 'X' && toe[7] == 'X' || toe[6] == 'X' && toe[3] == 'X')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
return 16;
}
}
if (lastmove == 3)
{
if (toe[5] == 'X' && toe[7] == 'X' || toe[5] == 'X' && toe[9] == 'X')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
return 17;
}
if (toe[5] == 'X' && toe[8] == 'X')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
return 18;
}
if (toe[5] == 'X' && toe[6] == 'X')
{
toe[4] = 'O'; cout << "Computer: 5" << endl;
return 19;
}
if (toe[5] == 'X' && toe[4] == 'X')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
return 20;
}
if (toe[5] == 'X' && toe[2] == 'X')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
return 21;
}
if (toe[5] == 'X' && toe[1] == 'X')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
return 22;
}
}
}
////////////////////////////////////////////////////////////////////////////THIRD MOVE//////////////////////////////////////////////////////////////////
int thirdmove()
{
if (lastmove == 1)
{
if (toe[7] == 'X' && toe[9] == 'X')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
return 1;
}
if (toe[9] == 'X' && toe[3] == 'X')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
return 2;
}
if (toe[3] == 'X' && toe[6] == 'X')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
}
}
if (lastmove == 2)
{
if (toe[8] == 'X' && toe[9] == 'X')
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
return 3;
}
if (toe[2] == 'O' && toe[5] == 'O' && toe[8] == ' ')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
return 4;
}
if (toe[8] == 'X' && toe[3] == 'X' || toe[8] == 'X' && toe[6] == 'X')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
return 5;
}
}
if (lastmove == 3)
{
if (toe[7] == ' ')
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
return 6;
}
if (toe[7] == 'X' && toe[9] == 'X')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
return 7;
}
if (toe[7] == 'X' && toe[1] == 'X')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
return 8;
}
}
if (lastmove == 4)
{
if (toe[6] == ' ')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
return 9;
}
if (toe[6] == 'X' && toe[1] == 'X')
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
return 10;
}
if (toe[2] == 'X' && toe[6] == 'X')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
return 11;
}
}
if (lastmove == 5)
{
if (toe[4] == ' ')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
return 12;
}
else
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
return 13;
}
}
if (lastmove == 6)
{
if (toe[3] == ' ')
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
return 14;
}
if (toe[3] == 'X' && toe[1] == 'X')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
return 15;
}
if (toe[3] == 'X' && toe[9] == 'X')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
return 16;
}
}
if (lastmove == 7)
{
if (toe[2] == ' ')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
return 17;
}
else
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
return 18;
}
}
if (lastmove == 8)
{
if (toe[1] == ' ')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
return 19;
}
if (toe[1] == 'X' && toe[3] == 'X')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
return 20;
}
if (toe[1] == 'X' && toe[7] == 'X')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
return 21;
}
}
if (lastmove == 9)
{
if (toe[9] == 'X' && toe[3] == 'X' || toe[9] == 'X' && toe[2] == 'X')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
return 22;
}
if (toe[9] == ' ')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
return 23;
}
if (toe[7] == 'X' && toe[9] == 'X')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
return 24;
}
}
if (lastmove == 10)
{
if (toe[8] == ' ')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
return 25;
}
else
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
return 26;
}
}
if (lastmove == 11)
{
if (toe[7] == 'X' && toe[6] == 'X')
{
if (toe[7] == 'X' && toe[9] == 'X')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
return 27;
}
else
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
return 27;
}
}
if (toe[1] == 'X' && toe[7] == 'X')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
return 27;
}
if (toe[7] == ' ')
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
return 28;
}
} ///// I removed 29 and 30 because they were already done in 27
if (lastmove == 12)
{
if (toe[6] == ' ')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
return 30;
}
if (toe[6] == 'X' && toe[7] == 'X')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
return 31;
}
if ( toe[8] == 'X' && toe[6] == 'X')
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
return 32;
}
}
if (lastmove == 13)
{
if (toe[4] == ' ')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
return 33;
}
if (toe[2] == 'X' && toe[4] == 'X')
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
return 34;
} ///// I removed 35 because it was the same as 34
}
if (lastmove == 14)
{
if (toe[3] == ' ')
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
return 36;
}
if (toe[3] == 'X' && toe[4] == 'X')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
return 37;
}
if (toe[3] == 'X' && toe[9] == 'X')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
return 38;
}
if (toe[1] == 'X' && toe[3] == 'X')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
return 39;
}
}
if (lastmove == 15)
{
if (toe[2] == ' ')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
return 40;
}
if (toe[2] == 'X' && toe[9] == 'X')
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
return 41;
}
if (toe[2] == 'X' && toe[6] == 'X')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
return 42;
}
}
if (lastmove == 16)
{
if (toe[1] == ' ')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
return 43;
}
if (toe[1] == 'X' && toe[3] == 'X')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
return 44;
}
if (toe[1] == 'X' && toe[7] == 'X')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
return 45;
}
}
if (lastmove == 17)
{
if (toe[2] == ' ')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
return 46;
}
if (toe[2] == 'X')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
return 47;
}
}
if (lastmove == 18)
{
if (toe[1] == ' ')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
return 48;
}
if (toe[1] == 'X')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
return 49;
}
}
if (lastmove == 19)
{
if (toe[2] == 'X')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
return 50;
}
if (toe[1] == 'X')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
return 51;
}
if (toe[7] == 'X' || toe[9] == 'X')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
return 52;
}
if (toe[8] == 'X')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
return 53;
}
}
if (lastmove == 20)
{
if (toe[9] == ' ')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
return 54;
}
if (toe[9] == 'X')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
return 55;
}
}
if (lastmove == 21)
{
if (toe[9] == 'X')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
return 56;
}
if (toe[6] == 'X')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
return 57;
}
if (toe[4] == 'X')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
return 58;
}
if (toe[7] == 'X' || toe[1] == 'X')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
return 59;
}
}
if (lastmove == 22)
{
if (toe[6] == ' ')
{
toe[6] = 'O';
cout << "Computer: 6" << endl;
return 60;
}
else
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
return 61;
}
}
}
////////////////////////////////////////////////////////////////////////////FOURTH MOVE//////////////////////////////////////////////////////////////////
void fourthmove()
{
if (lastmove == 1)
{
if (toe[2] == ' ')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
}
else
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
}
}
if (lastmove == 2)
{
if (toe[4] == ' ')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
}
else
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
}
}
if (lastmove == 3)
{
if (toe[3] == ' ')
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
}
else
{
toe[6] = 'O'; cout << "Computer: 3" << endl;
}
}
if (lastmove == 4)
{
cout << "You Lose" << endl;
}
if (lastmove == 5)
{
if (toe[9] == 'O' && toe[5] == 'O' && toe[1] == ' ')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
if (toe[6] == ' ' && toe[4] == ' ' || toe[1] == 'X' && toe[7] == 'X' || toe[4] == ' ' && toe[7] == ' ')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
}
if (toe[4] == 'X' && toe[1] == 'X')
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
}
} /// If (lastmove == 6) is missing because there is nothing to do after O wins
if (lastmove == 7)
{
if (toe[2] == ' ')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
}
else
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
}
if (lastmove == 8)
{
if (toe[6] == ' ')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
}
else
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
}
} /// If (lastmove == 9) is missing because there is nothing to do after O wins
if (lastmove == 10)
{
if (toe[9] == 'X')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
}
if (toe[8] == 'X' || toe[2] == 'X')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
}
if (toe[8] == 'X' && toe[9] == 'X')
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
}
if (toe[1] == 'X' && toe[2] == 'X')
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
}
}
if (lastmove == 11)
{
if (toe[2] == 'X' && toe[3] == 'X')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
if (toe[1] == 'X' && toe[2] == 'X')
{
toe[3] == 'O'; cout << "Computer: 3" << endl;
}
if (toe[8] == 'X')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
} /// If (lastmove == 12) is missing because there is nothing to do after O wins
if (lastmove == 13)
{
if (toe[7] == 'X' && toe[9] == 'X')
{
toe[8] = 'O'; cout << "Computer: " << endl;
}
if (toe[8] == 'X' && toe[9] == 'X')
{
toe[7] = 'O'; cout << "Computer: " << endl;
}
if (toe[7] == 'X' && toe[8] == 'X')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
}
} /// If (lastmove == 14) is missing because there is nothing to do after O wins
if (lastmove == 15)
{
if (toe[8] == ' ')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
}
else
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
}
}
if (lastmove == 16)
{
if (toe[4] == ' ')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
}
else
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
} /// If (lastmove == 17) is missing because there is nothing to do after O wins
if (lastmove == 18)
{
if (toe[4] == ' ')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
}
else
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
} /// If (lastmove == 19) is missing because there is nothing to do after O wins
if (lastmove == 20)
{
if (toe[8] == ' ')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
}
else
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
}
}
if (lastmove == 21)
{
if (toe[6] == ' ')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
}
else
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
}
}
if (lastmove == 22)
{
if (toe[4] == ' ')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
}
else ////// I wasn't sure if "else if(....)" would do the same, so I put the if inside the else
{
if (toe[7] == ' ')
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
}
else
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
}
}
} /// If (lastmove == 23) is missing because there is nothing to do after O wins
if (lastmove == 24)
{
if (toe[2] == ' ')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
}
else
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
}
} /// If (lastmove == 25) is missing because there is nothing to do after O wins
if (lastmove == 26)
{
if (toe[7] == ' ')
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
}
else
{
if (toe[4] == ' ')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
}
else
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
}
}
if (lastmove == 27)
{
if (toe[9] == ' ')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
}
if (toe[2] == ' ')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
}
else
{
if (toe[1] == ' ')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
}
if (toe[8] == ' ' && toe[9] == 'X')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
}
} /// If (lastmove == 28) is missing because there is nothing to do after O wins
if (lastmove == 31)
{
if (toe[1] == ' ')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
else
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
}
}
if (lastmove == 32)
{
if (toe[7] == ' ')
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
}
else
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
}
} /// If (lastmove == 33) is missing because there is nothing to do after O wins
if (lastmove == 34)
{
if (toe[3] == ' ')
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
}
else
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
} /// If (lastmove == 36) is missing because there is nothing to do after O wins
if (lastmove == 37)
{
if (toe[9] == ' ')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
}
else
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
}
}
if (lastmove == 38)
{
if (toe[4] == ' ')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
}
else
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
}
if (lastmove == 39)
{
if (toe[8] == ' ')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
}
else
{
toe[9] = 'O';
}
} /// If (lastmove == 40) is missing because there is nothing to do after O wins
if (lastmove == 41)
{
if (toe[7] == ' ')
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
}
else
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
}
if (lastmove == 42)
{
if (toe[9] == ' ')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
}
else
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
}
} /// If (lastmove == 43) is missing because there is nothing to do after O wins
if (lastmove == 44)
{
if (toe[8] == ' ')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
}
else
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
}
}
if (lastmove == 45)
{
if (toe[6] == ' ')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
}
else
{
toe[3] = 'O'; cout << "Computer: 3" << endl;
}
} /// If (lastmove == 46) is missing because there is nothing to do after O wins
if (lastmove == 47)
{
if (toe[6] == ' ')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
}
else
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
}
} /// If (lastmove == 48) is missing because there is nothing to do after O wins
if (lastmove == 49)
{
if (toe[6] == ' ')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
}
else
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
}
}
if (lastmove == 50)
{
if (toe[9] == ' ')
{
toe[9] = 'O'; cout << "Computer: 89" << endl;
}
else
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
}
if (lastmove == 51)
{
if (toe[8] == ' ')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
}
else
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
}
}
if (lastmove == 52)
{
if (toe[2] == ' ')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
}
else
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
}
}
if (lastmove == 53)
{
if (toe[1] == ' ')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
else
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
}
} /// If (lastmove == 54) is missing because there is nothing to do after O wins
if (lastmove == 55)
{
if (toe[2] == ' ')
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
}
else
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
}
}
if (lastmove == 56)
{
if (toe[4] == ' ')
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
}
else
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
}
}
if (lastmove == 57)
{
if (toe[1] == ' ')
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
else
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
}
}
if (lastmove == 58)
{
if (toe[9] == ' ')
{
toe[9] = 'O'; cout << "Computer: 9" << endl;
}
else
{
toe[1] = 'O'; cout << "Computer: 1" << endl;
}
}
if (lastmove == 59)
{
if (toe[7] == ' ')
{
toe[7] = 'O'; cout << "Computer: 7" << endl;
}
else if (toe[6] == ' ')
{
toe[6] = 'O'; cout << "Computer: 6" << endl;
}
else
{
toe[4] = 'O'; cout << "Computer: 4" << endl;
}
} /// If (lastmove == 60) is missing because there is nothing to do after O wins
if (lastmove == 61)
{
if (toe[8] == ' ')
{
toe[8] = 'O'; cout << "Computer: 8" << endl;
}
else
{
toe[2] = 'O'; cout << "Computer: 2" << endl;
}
}
}
////////////////////////////////////////////////////////////////////////////AUTO-MOVE//////////////////////////////////////////////////////////////////
void automove() // this checks for all blank spots and fills them in with X's
{
if (toe[1] == ' ')
{
toe[1] = 'X'; cout << "Auto-Fill: 1" << endl;
}
if (toe[2] == ' ')
{
toe[2] = 'X'; cout << "Auto-Fill: 2" << endl;
}
if (toe[3] == ' ')
{
toe[3] = 'X'; cout << "Auto-Fill: 3" << endl;
}
if (toe[4] == ' ')
{
toe[4] = 'X'; cout << "Auto-Fill: 4" << endl;
}
if (toe[5] == ' ')
{
toe[5] = 'X'; cout << "Auto-Fill: 5" << endl;
}
if (toe[6] == ' ')
{
toe[6] = 'X'; cout << "Auto-Fill: 6" << endl;
}
if (toe[7] == ' ')
{
toe[7] = 'X'; cout << "Auto-Fill: 7" << endl;
}
if (toe[8] == ' ')
{
toe[8] = 'X'; cout << "Auto-Fill: 8" << endl;
}
if (toe[9] == ' ')
{
toe[9] = 'X'; cout << "Auto-Fill: 9" << endl;
}
}
////////////////////////////////////////////////////////////////////////////WIN CHECK//////////////////////////////////////////////////////////////////
int wincheck()
{
if (toe[7] == 'O' && toe[8] == 'O' && toe[9] == 'O' || toe[4] == 'O' && toe[5] == 'O' && toe[6] == 'O' || toe[1] == 'O' && toe[2] == 'O' && toe[3] == 'O' || toe[7] == 'O' && toe[4] == 'O' && toe[1] == 'O' ||
toe[8] == 'O' && toe[5] == 'O' && toe[2] == 'O' || toe[9] == 'O' && toe[6] == 'O' && toe[3] == 'O' || toe[7] == 'O' && toe[5] == 'O' && toe[3] == 'O' || toe[9] == 'O' && toe[5] == 'O' && toe[1] == 'O')
{
losecount++; cout << "You Lose!" << endl;
return 1;
}
return 0;
}
|
|
|
01-28-2005, 08:15 PM
|
#8 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
I'll review it tomorrow (late here) most likely.
__________________
|
|
|
01-29-2005, 01:19 PM
|
#9 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Hmmm... you hate for loops do you? 
Let's see what I can do you for...
__________________
|
|
|
01-29-2005, 10:38 PM
|
#10 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
Quote:
|
Originally Posted by Valmont
Hmmm... you hate for loops do you? 
Let's see what I can do you for...
|
hate loops? what do you mean? the whole thing is in two loops.
|
|
|
01-29-2005, 10:56 PM
|
#11 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Lol we need more loops  .
Here is my code, where I tried to uphold your logic as much as I could. But obviously I optimized it. Check it out and see what you can do with it. There is tons of improvement left.
Code:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
enum Player{ HUMAN, AI } ;
Player activePlayer = HUMAN;
int losecount(0), tiecount(0), wincount(0);
string name;
//Name of array which places X's and O's. Initialise all fields with ' '.
char toe[9]={' '};
void resetboard();
//Human move.
void human();
void drawboard();
// Validates human input.
bool valcheck(int entry);
//Did a player win by any chance?
bool win(Player x);
//Is there a forced winning move or defending move (in this order)?
bool force_win_lose();
//Do a random move, center first, then random corner, otherwise random side.
void do_random();
//Is the board full?
bool board_full();
//Are the two fields equal, where "compare" = 'X' or 'Y'.
bool equal(char x, char y, char compare);
void swap_player(Player& plr);
void make_move(Player plr);
void intro();
void wrap_up();
//--
int main()
{
intro();
while(1)
{
srand( static_cast<unsigned>(time(0)) );
resetboard();
drawboard();
while(1)
{
make_move(activePlayer);
drawboard();
if(win(activePlayer) )
{
//We have a winner.
if(activePlayer == HUMAN)
{
cout<<"You win!"<<endl;
++wincount;
}
else //So the AI must have won.
{
cout<<"You lose!"<<endl;
++losecount;
}
break;
}
//No winner, perhaps board is full, therefore a tie?
if(board_full() )
{
cout<<"It is a tie!"<<endl;
++tiecount;
break;
}
//No winners, no tie. Move on to next move.
swap_player(activePlayer);
}
char yesno;
cout << "Would you like to play again? (y or n) \t";
cin >> yesno;
if (yesno == 'y' || yesno == 'Y')
{
system("CLS");
}
else
{
break;
}
}
wrap_up();
system("PAUSE");
return 0;
}
//---------------------------------------------------
void intro()
{
cout << "TIC-TAC-TOE" << endl << endl
<< "You are 'X'. Use the number pad to place your O's." << endl
<< "Please enter your name: ";
cin >> name;
cout<< "When you see - "<< name <<": - that means it is your turn to move."
<< endl << "Good Luck!" << endl;
char beginner('3');
while(1)
{
cout<<"Who should begin? (0 = Human, 1 = Computer) => ";
cin>>beginner;
if(beginner == '0')
{
activePlayer = HUMAN;
break;
}
else if(beginner == '1')
{
activePlayer = AI;
break;
}
}
}
//---------------------------------------------------
void make_move(Player plr)
{
if(plr == HUMAN)
{
human();
return;
}
else
{
//Can the AI do a forced winnning move, or a forced defending move??
if( !force_win_lose() )
{
//No forced win or defend. Do a random move.
do_random();
}
cout<<"\t\t\tComputer's move:"<<endl;
}
}
///////////DRAW BOARD///////////////
void drawboard()
{
cout << endl
<< "\t\t\t " << toe[7] << " | " << toe[8] << " | " << toe[9] << endl
<< "\t\t\t---|---|---" << endl
<< "\t\t\t " << toe[4] << " | " << toe[5] << " | " << toe[6] << endl
<< "\t\t\t---|---|---" << endl
<< "\t\t\t " << toe[1] << " | " << toe[2] << " | " << toe[3] << endl
<< endl << endl << endl;
}
/////////////Board Reset/////////////////////
void resetboard()
{
for( int i = 1; i < 10; ++i )
{
toe[i] = ' ';
}
}
/////////////HUMAN INPUT/////////////////////
void human()
{
int num;
cout << name << ": ";
cin >> num;
//the valid result in integrated in the if statements
while(!valcheck(num))
{
cout<<"Invalid move! Try again."<<endl;
cout << name << ": ";
cin >> num;
}
toe[num] = 'X';
}
//////////////////////VALIDATOR////////////////////////
bool valcheck(int entry)
{
// because all possible moves were set to spaces,
//this enables me to use an if statement say
//"if toe[?] == to a blank spot...." instead of "if toe[?] != to X or O
if(toe[entry] == ' ')
{
return true;
}
return false;
}
///////////////WIN CHECK/////////
bool win(Player x)
{
char symb;
if(x==HUMAN)
symb = 'X';
else
symb = 'O';
if
(
(toe[1] == symb && ((toe[2] == symb && toe[3] == symb)
|| (toe[4] == symb && toe[7] == symb)))
|| (toe[5] == symb && ((toe[4] == symb && toe[6] == symb)
|| (toe[2] == symb && toe[8] == symb)))
|| (toe[3] == symb && ((toe[6] == symb && toe[9] == symb)
|| (toe[5] == symb && toe[7] == symb)))
|| (toe[9] == symb && ((toe[7] == symb && toe[8] == symb)
|| (toe[1] == symb && toe[5] == symb)))
)
{
//Somebody won.
return true;
}
return false;
}
//--------------------------------
bool force_win_lose()
{
char comp;
int turn(1);
//Just making it readable a bit.
char a, b, c, d, e, f, g, h, i;
a = toe[1]; b = toe[2]; c = toe[3]; d = toe[4]; e = toe[5];
f = toe[6]; g = toe[7]; h = toe[8]; i = toe[9];
while(turn != 3)
{
//Check | | | |