I was able to display 3x3 gameboard with *, assign player 1-X palyer 2-O taking turns to place their marks. I don't know how to use bool to end game with one of the player WIN or TIE.
Please point me to the right direction.
#include "stdafx.h"
#include <iostream>
using namespace std;
void showBoard(void);
void playerInput(int p);
void checkWinner();
void nextPlayer(int);
int board[3][3]={{0,0,0},{0,0,0},{0,0,0}};
int _tmain(int argc, _TCHAR* argv[])
{
int r;
int c;
int player;
int winner;
int turns;
cout << "******* Tic Tac Toe Game *******" << endl;
showBoard();
nextPlayer(1);
return 0;
}
void showBoard(void)
{
int r;
int c;
cout << endl << endl;
cout <<"\tColumns\n";
cout <<"\t123\n";
for(r=0; r<=2; r++)
{
cout <<"Row " << (r+1) << ": ";
for(c=0; c<=2; c++)
{
if( board [r][c]==0)
cout << "* ";
else if (board [r][c]==1)
cout << "X ";
else
cout << "O ";
}
cout << endl;
}
}
void playerInput(int p)
{
int row;
int col;
if(p==1)
cout <<"\nPlayer 1, place X mark in the game board\n\n";
else
cout <<"\nPlayer 2, place O mark in the game board\n\n";
cout <<"Enter Row ";
cin >> row;
cout <<"Enter Column ";
cin >> col;
if(p==1)
board[--row][--col]=1;
if(p==2)
board[--row][--col]=2;
}
void checkWinner()
{
int winner;
for (int i=0; i<=2; i++)
{
if(board[i][0]==board[i][1] && board[i][1]==board[i][2] && board[i][0]!=0)
{
winner=board[i][0];
}
}
for(int i=0; i<=2; i++)
{
if(board[0][i]==board[1][i] && board[1][i]==board[2][i] && board[0][i]!=0)
{
winner=board[0][i];
}
}
if(board[0][0]=board[1][1] && board[1][1]==board[2][2] && board[0][0]!=0)
{
winner=board[0][0];
}
if(board[0][2]=board[1][1] && board[1][1]==board[2][0] && board [0][2]!=0)
{
winner=board[0][2];
}
if(board[0][0]==board[0][1] && board[0][1]==board[0][2]&& board[0][2]==board[0][1]&& board[1][0]==board [1][1]&& board[1][1]==board [1][2]&& board[1][2]==board[2][0]&&board[2][0]==board [2][1]&& board[2][1]==board [2][2] && board [0][0]!=0)
{
winner=0;
}
}
void nextPlayer(int player)
{
playerInput(player);
showBoard();
if(player==1)
player=2;
else
player=1;
nextPlayer(player);
}
This post has been edited by Martyn.Rae: 25 February 2011 - 10:49 PM
Reason for edit:: Added code tags

New Topic/Question
Reply




MultiQuote





|