I have been working on this assignment for a while. I have read a couple other threads in this site regarding similar solutions but I feel mine might be a little more unique. This tic-tac-toe board is user entered, simply setup as 9 characters in 3x3 format.
Input should look something like this:
xxo
.ox
o.x
The above would output, as well as the actual board itself(something similar to what I have shown below):
x | x | x
---------
| o | x
---------
o | | x
Then determine the winner. After it determines the winner, it will ask if the player would like to play another game.
This is not a 2 player game. It's quite simple, but quite frustrating and I'm a bit stuck. My code is not working. Take a look at what I have. It may seem all over the place...any suggestions are much appreciated. Thank you.
#include <cstdlib>
#include <iostream>
using namespace std;
void input (char board [3][3]);
void print (char board [3][3]);
bool win (char board [3][3], char player);
int main(int argc, char *argv[])
{
char board [3][3]= {0}; //tic tac toe board
int row, col; //current row and column in the array
char x,o ;
char space; //characters to be placed into the board
char player; //player of the tic tac toe game
char play; //play again
bool game_over; //game over
while (play != 'n');
{
input (board);
print (board);
win (board, player);
//Determines winner of the game
if (win (board, 'x'))
cout << "x wins!";
else if(win (board, 'o'))
cout << "o wins!";
else
cout << "No winner!";
//Prompts user if want to play again
cout << "Would you like to play again (y or n)?: "<<endl;
cin >> play;
if (play == 'y')
{
game_over = false; //game will continue.
}
else if (play == 'n')
{
game_over = true; //game will not continue
cout << "Thanks for playing!" << endl;
return 1;
}
else
{
cout << "Invalid answer! Try again!" << endl;
cin >> play;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
//What is being read in from the user
void input (char board [3][3])
{
char r,c; //current row and col
cout << "Enter your board as characters (x, o, .): "<< endl;
for (r = 0; r < 3; r++)
{
for (char c = 0; c < 3; c++)
{
cin >> board [r][c];
}
}
}
//what is being printed
void print (char board [3][3])
{
char r,c; //current row and col
for (r = 0; r < 3; r++)
{
for (c = 0; c < 3; c++)
cout << " " << board[r][c];
}
//This is the start of the board setup...I got stuck here and commented it out.
/*
if (board[r][c] = 'x')
{cout << " | " << board[r][c];}
if (r < 2)
{cout << " | " << board[r][c];}
cout << "/n---------";
if (board[r][c] == '.')
cout << " ";
if (board[r][c] == 'x')
cout << " | "<<
if (board [r][c] == 'o')
cout << " | ";
if (board [r][c] < 2)
cout << " ";
*/
//Decides who wins the game, 'x' or 'o' or no winner
bool win (char board [3][3], char player)
{
if
((board[0][0]==player && board[0][1]==player && board[0][2]==player)
return true;
else if(board[1][0]==player && board[1][1]==player && board[1][2]==player)
return true;
else if(board[2][0]==player && board[2][1]==player && board[2][2]==player)
return true;
else if(board[0][0]==player && board[1][0]==player && board[2][0]==player)
return true;
else if(board[0][1]==player && board[1][1]==player && board[2][1]==player)
return true;
else if(board[0][2]==player && board[1][2]==player && board[2][2]==player)
return true;
else if(board[0][0]==player && board[1][1]==player && board[2][2]=='player
return true;
else if(board[0][2]==player && board[1][1]==player && board[2][0]==player)
return true;
else if(board[2][0]==player && board[1][1]==player && board[0][2]==player));
return true;
else
return false;