about the game tic tac toe

what is the problem of the program? why is it not going to view when i

Page 1 of 1

5 Replies - 625 Views - Last Post: 04 October 2008 - 11:55 AM Rate Topic: -----

#1 Noparies_CpE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-September 08

about the game tic tac toe

Post icon  Posted 04 October 2008 - 07:19 AM

#include <iostream>
#include <conio.h>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using namespace std;



void printBoard(char[3][3], int, int);
int getRow(void);
int getCol(void);
void playerOMove(char array[][3]);
void playerXMove(char array[][3]);
bool checkOWin(char array[][3]);
bool checkXWin(char array[][3]);

int main ()
{
// Named constants for array dimensions
const int ROWS = 3;
const int COLUMNS = 3;

// Initialize Board
char board[ROWS][COLUMNS] = {{'-','-','-'},
{'-','-','-'},
{'-','-','-'}};

bool isWinner = false;
int counter = 0;
printBoard(board, ROWS, COLUMNS);

do {

// Allow player X to move, check if X wins, and print the board
playerXMove(board);
isWinner = checkXWin(board);
// If X wins, print the board and exit loop
if (isWinner) { printBoard(board, ROWS, COLUMNS); break; }
printBoard(board, ROWS, COLUMNS);

// only player X gets to move on last iteration. board should then be full
if (counter < 4) {
// Allow player O to move, check if O wins, and print the board
playerOMove(board);
isWinner = checkOWin(board);
// If O wins, print the board and exit loop
if (isWinner) { printBoard(board, ROWS, COLUMNS); break; }
printBoard(board, ROWS, COLUMNS);
}

counter++;
} while (counter < 5);

if (!isWinner) cout << "Tie! No Winner, Great Game!" << endl;

return 0;
}

bool checkXWin(char array[][3])
{
bool winner = false;
if ( array[0][0] + array[0][1] + array[0][2] == 264
|| array[1][0] + array[1][1] + array[1][2] == 264
|| array[2][0] + array[2][1] + array[2][2] == 264
|| array[0][0] + array[1][0] + array[2][0] == 264
|| array[0][1] + array[1][1] + array[2][1] == 264
|| array[0][2] + array[1][2] + array[2][2] == 264
|| array[0][0] + array[1][1] + array[2][2] == 264
|| array[0][2] + array[1][1] + array[2][0] == 264 )
winner = true;
if (winner) cout << "Player X Wins!\nGreat Job!" << endl;
return winner;
getch();
}

bool checkOWin(char array[][3])
{
bool winner = false;
if ( array[0][0] + array[0][1] + array[0][2] == 237
|| array[1][0] + array[1][1] + array[1][2] == 237
|| array[2][0] + array[2][1] + array[2][2] == 237
|| array[0][0] + array[1][0] + array[2][0] == 237
|| array[0][1] + array[1][1] + array[2][1] == 237
|| array[0][2] + array[1][2] + array[2][2] == 237
|| array[0][0] + array[1][1] + array[2][2] == 237
|| array[0][2] + array[1][1] + array[2][0] == 237 )
winner = true;
if (winner) cout << "Player O Wins!\nGreat Job!" << endl;
return winner;
getch();
}

// Asks user to input what row for move
int getRow(void)
{
int row;

do {
cout << "What row to make move?" << endl;
cin >> row;
// check for valid input
if (row > 2 || row < 0) cout << "Incorrect row, please choose between 0, 1, or 2." << endl;
} while (row > 2 || row < 0);

return row;
}

// Asks user to input what column for move
int getCol(void)
{
int col;

do {
cout << "What column to make move?" << endl;
cin >> col;
// check for valid input
if (col > 2 || col < 0) cout << "Incorrect column, please choose between 0, 1, or 2." << endl;
} while (col > 2 || col < 0);

return col;
}

// Allows Player 'X' to move
void playerXMove(char array[][3])
{
while (true) {
cout << "Pleyer X's turn" << endl;
int row = getRow();
int col = getCol();
// check if space is taken
if (array[row][col] == '-') { array[row][col] = 'X'; break; }
cout << "Space already occupied, please select another." << endl;
}
}

// Allows Player 'O' to make a move
void playerOMove(char array[][3])
{
while (true) {
cout << "Pleyer O's turn" << endl;
int row = getRow();
int col = getCol();
// check if space is taken
if (array[row][col] == '-') { array[row][col] = 'O'; break; }
cout << "Space already occupied, please select another." << endl;
}
}

// Function to print boad at whatever state it is in
void printBoard(char array[][3], int rows, int cols)
{
cout << "Heres the board:" << endl;

for (int i =0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)

cout << array[i][j] << " ";
cout << endl;
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: about the game tic tac toe

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: about the game tic tac toe

Posted 04 October 2008 - 07:37 AM

Edit post to include your code in the proper tags ( :code: ) and restate your question in the body of the post itself; in particular, what is the problem you are facing, what errors are there, etc.
Was This Post Helpful? 0
  • +
  • -

#3 Sadaiy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 03-October 08

Re: about the game tic tac toe

Posted 04 October 2008 - 07:57 AM

Hi, I made my own tic tac toe game if you would like to see, i have included code below.


Here is the .h first

class Board
{
private:
	enum Player {X, O, empty};
	Player board[3][3];
public:
	/** default constructor */
	Board();
	/** @purpose is to place either X or O in a specific row and column
	@pre the board, the position and the player X or O
	@post X or O is placed
	@param char player, int row, int column
	@return void */
	void place(char, int, int);
	/** @purpose is to determine if any player has won
	@pre a board with moves made
	@post true or false is returned
	@param char player
	@return bool */
	bool win(char);
	/** @purpose is to determine stalemate
	@pre a board that is full
	@post stalemate value is true or faluse
	@param none
	@return bool */
	bool stalemate();
	/** @purpose is show board
	@pre a board has been created
	@post board is displayed
	@param none
	@return void */
	void showBoard();
};



here is the implementation file

#include <iostream>
#include "board.h"

using namespace std;

Board::Board()
{
	for(int i = 0; i < 3; i++)
		for(int j = 0; j < 3; j++)
			board[i][j] = empty;
}

void Board::place(char p, int i, int j)
{
	if(board[i][j] == empty)
	{
		if(p == 'X')
			board[i][j] = X;

		if(p == 'O')
			board[i][j] = O;
	}
	else
		cout << endl << "Player " << p << " loses a turn because that spot has already been played!" << endl << endl;
}

bool Board::win(char x)
{	
	Player p;
	if( x == 'X')
		p = X;
	if(x == 'O')
		p = O;

	if((board[0][0] == p) && (board[1][0] == p) && (board[2][0] == p))
		return true;
	else if((board[0][0] == p) && (board[0][1] == p) && (board[0][2] == p))
		return true;
	else if((board[0][1] == p) && (board[1][1] == p) && (board[1][2] == p))
		return true;
	else if((board[0][2] == p) && (board[1][2] == p) && (board[2][2] == p))
		return true;
	else if((board[0][0] == p) && (board[0][1] == p) && (board[0][2] == p))
		return true;
	else if((board[1][0] == p) && (board[1][1] == p) && (board[1][2] == p))
		return true;
	else if((board[2][0] == p) && (board[2][1] == p) && (board[2][2] == p))
		return true;
	else if((board[0][0] == p) && (board[1][1] == p) && (board[2][2] == p))
		return true;
	else if((board[0][2] == p) && (board[1][1] == p) && (board[2][0] == p))
		return true;
	else
		return false;
}

bool Board::stalemate()
{
	if(((board[0][0] != empty) && (board[0][1] != empty) && (board[0][2] != empty) && (board[1][0] != empty) && (board[1][1] != empty) && (board[1][2] != empty) && (board[2][0]) && (board[2][1] != empty) && (board[2][2] != empty)) && (win('X') == false) && (win('O') == false))
		return true;
	else
		return false;
}

void Board::showBoard()
{
	cout << "Tic Tac Toe Board: " << endl << endl;
	for(int i = 0; i < 3; i++)
	{
		for(int j = 0; j < 3; j++)
		{
			if(board[i][j] == empty)
				cout << "empty" << "[" << i+1 << "]" << "[" << j+1 << "]" << "  ";
			
			if(board[i][j] == O)
				cout << "	 O	 " << "  ";
			
			if(board[i][j] == X)
				cout << "	 X	 " << "  ";
		}
		cout << endl;
	}
	cout << endl;
}



ok and here is the driver file

#include <iostream>
#include "board.h"

using namespace std;

int main()
{
Board test;
int r, c;
char p = 'A';

int playerTurnCounter = 1;

test.showBoard();

while((test.win('X') != true) && (test.win('O') != true) && (test.stalemate() != true))
{
	if(playerTurnCounter % 2 == 1)
	{
		p = 'X';
		cout << "Player X's turn!" << endl << endl;
	}
	else
	{
		p = 'O';
		cout << "Player O's turn!" << endl << endl;
	}
	
	cout << "Please enter the row to place for player " << p << " (1-3): " << endl;
	cin >> r;
	cout << "Please enter the column to place for player " << p << " (1-3): " << endl;
	cin >> c;

	test.place(p, r - 1, c - 1);

	test.showBoard();

	playerTurnCounter++;
}

if(test.win('X') == true)
	cout << "Player X wins!" << endl;

if(test.win('O') == true)
	cout << "Player O wins!" << endl;

if(test.stalemate() == true)
	cout << "Game ends in stalemate!" << endl;

return 0;
}

Was This Post Helpful? 0
  • +
  • -

#4 Sadaiy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 03-October 08

Re: about the game tic tac toe

Posted 04 October 2008 - 09:33 AM

What's the problem with your code ? when i put it into my compiler (visual studio 2008) it works fine for me.. .only problem is i think you have to add +1 to the row and column because right the player has to enter 0 for the first row / column....
Was This Post Helpful? 0
  • +
  • -

#5 Noparies_CpE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-September 08

Re: about the game tic tac toe

Posted 04 October 2008 - 11:05 AM

The problem with my code is when there a winner, the command that i put like
if (winner) cout << "Player O Wins!\nGreat Job!" << endl; doesn`t work..
if there is already a winner it just simply terminates the program..
it doesnt say that Player O wins or Player X wins..it just terminates when there is a winner.. why? is there any problem in my cout?
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5940
  • View blog
  • Posts: 12,866
  • Joined: 16-October 07

Re: about the game tic tac toe

Posted 04 October 2008 - 11:55 AM

View PostNoparies_CpE, on 4 Oct, 2008 - 02:05 PM, said:

if (winner) cout << "Player O Wins!\nGreat Job!" << endl; doesn`t work..
if there is already a winner it just simply terminates the program..
it doesnt say that Player O wins or Player X wins..it just terminates when there is a winner.. why? is there any problem in my cout?


Works fine, I think. You're just not seeing it.
winner = true;
if (winner) cout << "Player O Wins!\nGreat Job!" << endl;
return winner;
getch();  // THIS IS NEVER RUN



Take the pause out of the function and put it at the end of the program, right before the end of main ( but before the return 0; ). See how that works.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1