Hello,
I've recently, inadvertently, found incentive to write my own Tic-Tac-Toe game. I've been working on it for more-or-less 15 minutes and would like some help or a point in the right direction. It is almost finished but there is a function, namely the "_scan4Winner" function, that I must write an algorithm or come up with a formula to check for a winning condition.
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
#define NWIN_CON 0
#define PONE_WIN 1
#define PTWO_WIN 2
#define GRST 2
const char playerOne = 'X';
const char playerTwo = 'O';
int _writeGrid( int y, int x, int flag );
int _scan4Winner(char (*grid)[3]);
int _playerWin(int winner);
int main()
{
char setLocation;
/* char hexc = 0x41; */
int randomee, turn, ret;
int px = 0, py = 0;
srand(time(NULL)); // Seed for random
// This will put a random number between 1 and 10 which will
// determine what player initially goes first
randomee = rand() % 10 + 1;
// Which player goes first
turn = (randomee >= 5) ? 1 : 0;
// Indicate which user goes first
cout << "Player " << turn << "is first" << endl;
// This will print a fresh new grid
_writeGrid(NULL, NULL, GRST);
// Start the actual game
while (1)
{
cout << "Player " << turn << " set: ";
cin >> setLocation;
switch (setLocation)
{
case 0x41 :
px = 0;
py = 0;
break;
case 0x42 :
px = 1;
py = 0;
break;
case 0x43 :
px = 2;
py = 0;
break;
case 0x44 :
px = 0;
py = 1;
break;
case 0x45 :
px = 1;
py = 1;
break;
case 0x46 :
px = 2;
py = 1;
break;
case 0x47 :
px = 0;
py = 2;
break;
case 0x48 :
px = 1;
py = 2;
break;
case 0x49 :
px = 2;
py = 2;
break;
}
/*************************************
while (hexc != 0x49)
{
if (setLocation == hexc)
{
ret = _writeGrid(py, px, turn);
hexc = 0x41;
px = 0;
py = 0;
break;
}
hexc++;
if (px < 2)
px++;
else
{
px = 0;
py++;
}
}
****************************************/
// Once the coords are determined pass them and which player is
// going, return and check if that player won.
ret = _writeGrid(px, py, turn);
if (ret == PONE_WIN)
{
turn = 0;
_writeGrid(NULL, NULL, GRST);
}
else if (ret == PTWO_WIN)
{
turn = 1;
_writeGrid(NULL, NULL, GRST);
}
else
turn = (turn) ? 0 : 1; // If no one won yet switch players
}
return 0;
}
// This function writes a 3 x 3 tic-tac-toe grid and adds an X or a O
// depending on the current user, and also takes a grid corrdinates
// that'll determine the position or the X or O before it prints the grid
int _writeGrid(int y, int x, int flag)
{
static char gridMap[3][3]; // Make static so grid is remembered
int i, j, winner;
char hexc = 0x41;
// If the flag is a reset flag, reset the whole grid
if (flag == GRST)
{
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
gridMap[i][j] = hexc;
hexc++;
cout << "| " << gridMap[i][j];
}
cout << endl << endl;
}
return GRST;
}
else
{
// Condition flag and place either an X(playerOne) or O
// (playerTwo) into the coords passed as parameters
gridMap[x][y] = (flag) ? playerOne : playerTwo;
system("cls");
// This section will actually write the grid using the values
// stored in gridMap
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cout << "| " << gridMap[i][j];
}
cout << endl << endl;
}
// The next step is to check for a winner so we pass gridMap
// containing the actual grid status winner = _scan4Winner
// (gridMap) If a winning condition is encountered notify the
// player who wins, add one point to his current score and reset
// the grid.
if (winner)
return _playerWin((flag) ? PONE_WIN : PTWO_WIN);
return NWIN_CON;
}
}
// This function will have a formula or algorition that'll check for
// winning conditions.
int _scan4Winner(char (*grid)[3])
{
return 0;
}
// This function simply prints whose the winner of a tic-tac-toe game
// and increments a counter variable for the corresponding user
int _playerWin(int winner)
{
static int pcount1;
static int pcount2;
if (winner == PONE_WIN)
{
pcount1++;
cout << "Player one wins - score is" << pcount1 << endl;
return PONE_WIN;
}
else
{
pcount2++;
cout << "Player two wins - score is" << pcount2 << endl;
return PTWO_WIN;
}
}
Thanks in advanced, LamaBot