Code:
#include <iostream.h>
#include <windows.h>
typedef unsigned short int USHORT;
char board[3][3];
char playername1[25];
char playername2[25];
char player1 = 'x';
char player2 = 'o';
char playerup = 'x';
USHORT i, ii, iii, x, y, moves, move;
void table(void);
int init(void);
int getInput(char player);
int setInput(char player);
USHORT checkWin(playerup)
int main ()
{
init();
table();
while (1)
{
if (moves <= 9)
{
if (checkWin(playerup) != 0 || checkWin(playerup) != 1)
{
getInput(playerup);
setInput(playerup);
SYSTEM("CLS");
playerup = 'o';
} else {
if (playerup == 'x')
{
cout << playername1 << " wins! Congratulations.";
table();
return 0;
} else {
cout << playername2 << "wins! Congratulations.";
table();
return 0;
}
}
} else {
cout << "No winner this time; thanks for playing!";
table();
return 0;
}
}
}
USHORT checkWin(playerup)
{
if (playerup == 'x')
{
if ((board[0][0] == board[0][1] && board[0][1] == board[0][2])
{
return 0;
}
if ((board[1][0] == board[1][1] && board[1][1] == board[1][2])
{
return 0;
}
if ((board[2][0] == board[2][1] && board[2][1] == board[2][2])
{
return 0;
}
//CHECK COLUMNS
if ((board[0][0] == board[1][0] && board[1][0] == board[2][0])
{
return 0;
}
if ((board[0][1] == board[1][1] && board[1][1] == board[2][1])
{
return 0;
}
if ((board[0][2] == board[1][2] && board[1][2] == board[2][2])
{
return 0;
}
//CHECK DIAGONALS
if ((board[0][2] == board[1][1] && board[1][1] == board[2][2])
{
return 0;
}
if ((board[2][0] == board[1][1] && board[1][1] == board[0][2])
{
return 0;
}
} else {
if ((board[0][0] == board[0][1] && board[0][1] == board[0][2])
{
return 1;
}
if ((board[1][0] == board[1][1] && board[1][1] == board[1][2])
{
return 1;
}
if ((board[2][0] == board[2][1] && board[2][1] == board[2][2])
{
return 1;
}
//CHECK COLUMNS
if ((board[0][0] == board[1][0] && board[1][0] == board[2][0])
{
return 1;
}
if ((board[0][1] == board[1][1] && board[1][1] == board[2][1])
{
return 1;
}
if ((board[0][2] == board[1][2] && board[1][2] == board[2][2])
{
return 1;
}
//CHECK DIAGONALS
if ((board[0][2] == board[1][1] && board[1][1] == board[2][2])
{
return 1;
}
if ((board[2][0] == board[1][1] && board[1][1] == board[0][2])
{
return 1;
}
}
return 2;
}
void table(void)
{
cout << "Current Table:\n";
cout << "+---+---+---+" <<endl;
cout << "| "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<" |" <<endl;
cout << "+---+---+---+" <<endl;
cout << "| "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<" |" <<endl;
cout << "+---+---+---+" <<endl;
cout << "| "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<" |" <<endl;
cout << "+---+---+---+" <<endl;
}
int init(void)
{
cout << "Welcome to Tic Tac Toe\n";
cout << "Player 1, enter your name now->";
cin.getline(*playername1, 25, '\n');
cout << "\nPlayer 2; enter yours->";
cin.getline(*playername2, 25, '\n');
for (i = 0; i < 3; i++)
{
for (ii = 0; ii < 3; ii++)
{
for (iii = 1; iii<=9; iii++)
board[i][ii] = (char)iii;
}
}
return 0;
}
int getInput(char player)
{
if (player == 'x')
{
cout << "Player 1, make your move->";
cin.getline(*move, 1, '\n');
switch (move)
{
case 1: { x = 0; y = 0; break; }
case 2: { x = 0; y = 1; break; }
case 3: { x = 0; y = 2; break; }
case 4: { x = 1; y = 0; break; }
case 5: { x = 1; y = 1; break; }
case 6: { x = 1; y = 2; break; }
case 7: { x = 2; y = 0; break; }
case 8: { x = 2; y = 1; break; }
case 9: { x = 2; y = 2; break; }
{
cout << "Invalid input, please restate->";
getInput(player);
}
}
return 1;
} else {
cout << "Player 2, make your move->";
cin.getline(*move, 1, '\n');
switch (move)
{
case 1: { x = 0; y = 0; break; }
case 2: { x = 0; y = 1; break; }
case 3: { x = 0; y = 2; break; }
case 4: { x = 1; y = 0; break; }
case 5: { x = 1; y = 1; break; }
case 6: { x = 1; y = 2; break; }
case 7: { x = 2; y = 0; break; }
case 8: { x = 2; y = 1; break; }
case 9: { x = 2; y = 2; break; }
{
cout << "Invalid input, please restate->";
getInput(player);
}
}
return 2;
}
}
int setInput(char player)
{
if (board[x][y] != 'x' || board[x][y] != 'o')
{
board[x][y] = player;
moves++;
return 0;
} else {
cout << "That move has been made, try again";
getInput(playerup);
}
}
I tried a few things, its still not working..