Hello all!! I have a quick question I am making a tic-tac-toe console game and it somewhat works the only thing that doesn't is checking vertical wins. I think my logic is wrong. Thanks for helping!!
//My Game.h File
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
constchar PLAYER = 'X';
constchar COMPUTER = 'O';
constchar EMPTY = '*';
constint PLAYING = -1;
constint PLAYER_WINS = 0;
constint COMPUTER_WINS = 1;
constint DRAW = 2;
constint MAP_WIDTH = 3;
constint MAP_HEIGHT = 3;
typedefstruct //Struct containing winning info
{
int iState;
char chWho;
}Win;
class Game
{
char Map [MAP_WIDTH][MAP_HEIGHT]; //Tic-Tac-Toe Map
public:
Game (); //Constructor
void drawMap (); //Draws map to console
void clearMap (); //Clears the map to an EMPTY value
void playerMove (); //Asks user for X and Y points
void computerMove (); //Generates a computer X and Y points.
Win gameState (); //Get current game state
};
//My Game.cpp File
Game::Game ()
{
this->clearMap ();
}
//Init the map to EMPTY
void Game::clearMap ()
{
int i = 0 , j = 0;
for (i = 0 ; i < MAP_WIDTH ; i++)
{
for (j = 0 ; j < MAP_HEIGHT ; j++)
{
this->Map [i][j] = EMPTY;
}
}
}
//Draw the map
void Game::drawMap ()
{
int i = 0 , j = 0;
for (i = 0; i < MAP_WIDTH ; i++)
{
for (j = 0; j < MAP_HEIGHT ; j++)
{
cout << this->Map [i][j] << " ";
}
cout << endl;
}
}
//Get player X and Y points
void Game::playerMove ()
{
int iPlayerX = 0 , iPlayerY = 0;
do
{
cout << "Please enter a Y value." << endl;
cout << "Y >> : ";
cin >> iPlayerX;
while (cin.fail ())
{
cout << "Please enter a number." << endl;
cout << "Y >> : ";
cin.clear ();
cin.ignore (256 , '\n');
cin >> iPlayerX;
}
cout << "Please enter a X value." << endl;
cout << "X >> : ";
cin >> iPlayerY;
while (cin.fail ())
{
cout << "Please enter a number." << endl;
cout << "X >> : ";
cin.clear ();
cin.ignore (256 , '\n');
cin >> iPlayerY;
}
}while (this->Map [iPlayerX][iPlayerY] != EMPTY);
this->Map [iPlayerX][iPlayerY] = PLAYER;
}
//Generate computer X and Y
void Game::computerMove ()
{
int iComputerX = 0 , iComputerY = 0;
do
{
if (this->gameState ().iState == DRAW)
{
break;
}
iComputerX = rand () % MAP_WIDTH;
iComputerY = rand () % MAP_HEIGHT;
}while (this->Map [iComputerX][iComputerY] != EMPTY);
this->Map [iComputerX][iComputerY] = COMPUTER;
}
//This is were I have my problems
Win Game::gameState ()
{
//State to be returned at the end of checking.
Win State = {PLAYING , '?'};
int i = 0 , j = 0; //Temp integers
int iCounter = 0; //Temp counter
for (i = 0 ; i < MAP_WIDTH ; i++) //This is suppose to check vertical wins
{
for (j = 0 ; j < MAP_HEIGHT ; j++)
{
if (j + 1 < MAP_HEIGHT) //To check for array out of bounds
{
if (this->Map [i][j] == PLAYER || this->Map [i][j] == COMPUTER) //If the spot is used
{
/*If one point is the same as the next then increment the iCounter and make the player on that spot the winner*/
if (this->Map [i][j] == this->Map [i][j + 1])
{
iCounter++;
State.chWho = this->Map [i][j];
}
}
}
}
if (iCounter == MAP_HEIGHT - 1) /*If all the spots were the same then determine who won*/
{
if (State.chWho == COMPUTER)
{
State.iState = COMPUTER_WINS;
return State;
}
else
{
State.iState = PLAYER_WINS;
return State;
}
}
else
{
iCounter = 0;
}
}
iCounter = 0;
//This checks horizontal wins
for (i = 0 ; i < MAP_HEIGHT ; i++)
{
for (j = 0 ; j < MAP_WIDTH ; j++)
{
if (j + 1 < MAP_WIDTH)
{
//Same logic as before if the spot is used then compare this and next
if (this->Map [i][j] == COMPUTER || this->Map [i][j] == PLAYER)
{
if (this->Map [i][j] == this->Map [i][j + 1])
{
iCounter++;
State.chWho = this->Map [i][j];
}
}
}
}
if (iCounter == MAP_WIDTH - 1)
{
if (State.chWho == COMPUTER)
{
State.iState = COMPUTER_WINS;
return State;
}
else
{
State.iState = PLAYER_WINS;
return State;
}
}
else
{
iCounter = 0;
}
}
iCounter = 0;
//Checks for a draw (every space is used)
for (i = 0 ; i < MAP_WIDTH ; i++)
{
for (j = 0 ; j < MAP_HEIGHT ; j++)
{
if (this->Map [i][j] == COMPUTER || this->Map [i][j] == PLAYER)
{
iCounter++;
}
}
}
if (iCounter == MAP_WIDTH * MAP_HEIGHT)
{
State.iState = DRAW;
State.chWho = 'D';
return State;
}
//None of the loops found anything therefore we must be playing
State.iState = PLAYING;
State.chWho = '?';
return State;
}
Well, check the line 104 and 143. They're the same but that can't be right. The horizontal advancing cannot be done in the same dimension like the vertical. It's somewhat confusing that you're using i and j similar but for completly different directions
@coder777 Oh I see I am using i and j twice. For the vertical check I'll use i instead of j. I'll report back my results. By the way congrats on your 1200th post.