Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,837 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,580 people online right now. Registration is fast and FREE... Join Now!




about the game tic tac toe

 
Reply to this topicStart new topic

about the game tic tac toe, what is the problem of the program? why is it not going to view when i

Noparies_CpE
4 Oct, 2008 - 06:19 AM
Post #1

New D.I.C Head
*

Joined: 17 Sep, 2008
Posts: 6

#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;
}
}

User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: About The Game Tic Tac Toe
4 Oct, 2008 - 06:37 AM
Post #2

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 597



Thanked: 60 times
Dream Kudos: 50
My Contributions
Edit post to include your code in the proper tags ( code.gif ) 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.
User is offlineProfile CardPM
+Quote Post

Sadaiy
RE: About The Game Tic Tac Toe
4 Oct, 2008 - 06:57 AM
Post #3

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 38



Thanked: 1 times
My Contributions
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

CODE

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

CODE

#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

CODE

#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;
}

User is offlineProfile CardPM
+Quote Post

Sadaiy
RE: About The Game Tic Tac Toe
4 Oct, 2008 - 08:33 AM
Post #4

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 38



Thanked: 1 times
My Contributions
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....
User is offlineProfile CardPM
+Quote Post

Noparies_CpE
RE: About The Game Tic Tac Toe
4 Oct, 2008 - 10:05 AM
Post #5

New D.I.C Head
*

Joined: 17 Sep, 2008
Posts: 6

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?
User is offlineProfile CardPM
+Quote Post

baavgai
RE: About The Game Tic Tac Toe
4 Oct, 2008 - 10:55 AM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,040



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
QUOTE(Noparies_CpE @ 4 Oct, 2008 - 02:05 PM) *

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.
cpp

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.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 05:11PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month