Tic-Tac-Toe Game in Dev

Using Code::Blocks

Page 1 of 1

6 Replies - 8408 Views - Last Post: 06 August 2009 - 06:03 PM Rate Topic: -----

#1 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Tic-Tac-Toe Game in Dev

Posted 15 April 2008 - 05:09 PM

I have been writing a Tic Tac Toe game in C++ using the Code Blocks IDE.
Its not done, I haven't added a sysytem for the computer to counter the players actions.
I juts want some help with error resolution, and coding advice.
Everything you need will be in an attachment. I compressed it to save DICs bandwidth.

Thanks for taking the time to review my code!! :)

Dream in code :-)

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Tic-Tac-Toe Game in Dev

#2 thea almira  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-May 08

Re: Tic-Tac-Toe Game in Dev

Posted 14 May 2008 - 09:23 AM

View PostDelta_Echo, on 15 Apr, 2008 - 05:09 PM, said:

I have been writing a Tic Tac Toe game in C++ using the Code Blocks IDE.
Its not done, I haven't added a sysytem for the computer to counter the players actions.
I juts want some help with error resolution, and coding advice.
Everything you need will be in an attachment. I compressed it to save DICs bandwidth.

Thanks for taking the time to review my code!! :)

Dream in code :-)




#include <iostream>



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

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

// 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;
}
}
Was This Post Helpful? 0
  • +
  • -

#3 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Tic-Tac-Toe Game in Dev

Posted 15 May 2008 - 05:01 AM

Thanks. :)
btw, please place code in the code tags : [*code] CODE HERE [*/code]

:) Thanks again!
Was This Post Helpful? 0
  • +
  • -

#4 Codegamer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 127
  • Joined: 04-May 08

Re: Tic-Tac-Toe Game in Dev

Posted 15 May 2008 - 06:19 AM

lol the code doesnīt work for me :blink:
Was This Post Helpful? 0
  • +
  • -

#5 cryptlord90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-August 09

Re: Tic-Tac-Toe Game in Dev

Posted 06 August 2009 - 04:48 PM

Hey guys , i currently working on C++ tic tac toe and was somehow doing the ai part , could anyone show me some past samples that you had ? thanks (:
Was This Post Helpful? 0
  • +
  • -

#6 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Tic-Tac-Toe Game in Dev

Posted 06 August 2009 - 05:22 PM

First make your own topic and then repost your question with some effort.
Was This Post Helpful? 0
  • +
  • -

#7 cryptlord90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-August 09

Re: Tic-Tac-Toe Game in Dev

Posted 06 August 2009 - 06:03 PM

no problem, but do come help me alright ? :X
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1