![]() |
| | #1 |
| Registered User Join Date: Apr 2009
Posts: 3
| Need a little assistance. The program should include the following class, properties and methods: Code: class TicTacToe{
private:
char theBoard [3][3];
public:
TicTacToe(void);
void playOneGame(void);
void switchPlayer(char &);
void showBoard(void);
void postMove(int, int, char);
char determineWinner(void);
};
int main (void){
//test the class by playing one game
TicTacToe Game1;
Game1.playOneGame();
}
void TicTacToe::playOneGame(void){
//start a game and play until someone wins or a draw occurs...
const int MaxMoves = 9;
char currentPlayer = 'O';
int row = 0;
int clmn = 0;
char theWinner = ' ';
int nmbrOfMoves = 0; //keep track of the number of moves max is 9
do {
switchPlayer(currentPlayer); //change player from x to o or vice versa
showBoard();
cout << "\n\nPlayer " << currentPlayer << endl; //get the players move
cout << "enter your row: ";
cin >> row;
cout << "enter your column: ";
cin >> clmn;
postMove(row, clmn, currentPlayer); //post the move to the board
theWinner = determineWinner(); //see if anyone won the game
nmbrOfMoves++; //keep track of the number of moves
} while ((theWinner == 'D') && (nmbrOfMoves < MaxMoves));
showBoard(); //show the ending board
if (theWinner != 'D') //declare a winner
cout << "\n\nThe Winner is player " << theWinner;
else
cout << "\n\nThe Game was a Draw";
}
TicTacToe::TicTacToe(void){
//intialize the array contents
}
void TicTacToe::switchPlayer(char ¤tPlayer){
//switches the current player
}
void TicTacToe::showBoard(){
//displays the board
}
void TicTacToe::postMove(int row, int col, char value){
//gets the users move and posts it to the board
}
char TicTacToe::determineWinner(void){
//analyzes the board to see if there is a winner
//returns a X, O indicating the winner
//if the game is a draw then D is returned
//check the rows
for (int i = 0; i < 3; i++){
if (theBoard[i][0] == theBoard[i][1]
&& theBoard[i][1] == theBoard[i][2]
&& theBoard[i][0] != ' '){
return theBoard[i][0];
}
}
//check the clmns
for (int i = 0; i < 3; i++){
if (theBoard[0][i] == theBoard[1][i]
&& theBoard[1][i] == theBoard[2][i]
&& theBoard[0][i] != ' '){
return theBoard[0][i];
}
}
//check the diagnals
if (theBoard[0][0] == theBoard[1][1]
&& theBoard[1][1] == theBoard[2][2]
&& theBoard[0][0] != ' ') {
return theBoard[0][0];
}
if (theBoard[2][0] == theBoard[1][1]
&& theBoard[1][1] == theBoard[0][2]
&& theBoard[2][0] != ' ') {
return theBoard[2][0];
}
return 'D';
}
[ ] [ ] [ ] [ ] [O] [ ] [ ] [ ] [X] Player X: Enter row of move (1, 2, 3): 3 Enter column of move (1, 2, 3): 1 This is what I have so far... Code: // Nathaniel Drake
// Lab 6 Excercise 2
// This lab creates and allows two users to play a friendly game of tic-tac-toe.
// Tic-tac-toe.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class TicTacToe{
private:
char theBoard [3][3];
public:
TicTacToe(void);
void playOneGame(void);
void switchPlayer(char &);
void showBoard(void);
void postMove(int, int, char);
char determineWinner(void);
};
int main (void){
//test the class by playing one game
TicTacToe Game1;
Game1.playOneGame();
}
void TicTacToe::playOneGame(void){
//start a game and play until someone wins or a draw occurs...
const int MaxMoves = 9;
char currentPlayer = 'O';
int row = 0;
int clmn = 0;
char theWinner = ' ';
int nmbrOfMoves = 0; //keep track of the number of moves max is 9
do {
switchPlayer(currentPlayer); //change player from x to o or vice versa
showBoard();
cout << "\n\nPlayer " << currentPlayer << endl; //get the players move
cout << "enter your row: ";
cin >> row;
cout << "enter your column: ";
cin >> clmn;
postMove(row, clmn, currentPlayer); //post the move to the board
theWinner = determineWinner(); //see if anyone won the game
nmbrOfMoves++; //keep track of the number of moves
} while ((theWinner == 'D') && (nmbrOfMoves < MaxMoves));
showBoard(); //show the ending board
if (theWinner != 'D') //declare a winner
cout << "\n\nThe Winner is player " << theWinner;
else
cout << "\n\nThe Game was a Draw";
}
TicTacToe::TicTacToe(void){
//intialize the array contents
int grid [3][3];
for(int i = 0; i < 3;i++)
for (int j = 0; j < 3; j++)
cout << grid [i][j] = 0;
}
void TicTacToe::switchPlayer(char ¤tPlayer){
//switches the current player
if currentPlayer == 'O'
cout << "Player 2 its your turn" << endl;
else currentPlayer == 'X'
cout << "Player 1 its your turn" << endl;
}
void TicTacToe::showBoard(){
cout << "[ ] [ ] [ ] \n";
cout << "[ ] [ ] [ ] \n";
cout << "[ ] [ ] [ ] \n";//displays the board
for(int i = 0; i < 3;i++)
for (int j = 0; j < 3; j++)
cout << grid [i][j] = 0;
}
void TicTacToe::postMove(int row, int col, char value){
//gets the users move and posts it to the board
cout << "Player 1 whats your move:" << postMove;
cin >> >> postMove;
cout << "Player 2 whats your move:" << postMove;
}
char TicTacToe::determineWinner(void){
//analyzes the board to see if there is a winner
//returns a X, O indicating the winner
//if the game is a draw then D is returned
//check the rows
for (int i = 0; i < 3; i++){
if (theBoard[i][0] == theBoard[i][1]
&& theBoard[i][1] == theBoard[i][2]
&& theBoard[i][0] != ' '){
return theBoard[i][0];
}
}
//check the clmns
for (int i = 0; i < 3; i++){
if (theBoard[0][i] == theBoard[1][i]
&& theBoard[1][i] == theBoard[2][i]
&& theBoard[0][i] != ' '){
return theBoard[0][i];
}
}
//check the diagnals
if (theBoard[0][0] == theBoard[1][1]
&& theBoard[1][1] == theBoard[2][2]
&& theBoard[0][0] != ' ') {
return theBoard[0][0];
}
if (theBoard[2][0] == theBoard[1][1]
&& theBoard[1][1] == theBoard[0][2]
&& theBoard[2][0] != ' ') {
return theBoard[2][0];
}
return 'D';
}
|
| drakenathaniel is offline | |
| | #2 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| And what do you need help with?
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #3 |
| Registered User Join Date: Apr 2009
Posts: 3
| hey im trying to see what is wrong with my code that it wont run and what should I do to fix it. |
| drakenathaniel is offline | |
| | #4 | |
| Banned Join Date: Mar 2009
Posts: 533
| Quote:
__________________ ╔╗╔══╦╗ ║║║╔╗║║ ║╚╣╚╝║╚╗ ╚═╩══╩═╝ | |
| ಠ_ಠ is offline | |
| | #5 |
| Registered User Join Date: Apr 2009
Posts: 3
| Great... Thanks I hope that works... ~sigh~ |
| drakenathaniel is offline | |
![]() |
| Tags |
| arrays, c++, homework, pointers, tic tac toe |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Hello,i need assistance in completing the code | toader | C++ Programming | 1 | 06-22-2009 03:32 AM |
| Variable Timer assistance | rich2852 | C Programming | 2 | 01-21-2009 05:43 AM |
| Any assistance would be greatly appreciated | iiwhitexb0iii | C Programming | 18 | 02-26-2006 12:06 PM |
| Need some more assistance | Thantos | Windows Programming | 6 | 08-14-2003 12:13 PM |
| Need assistance for major text base Arena RPG project | Ruflano | C++ Programming | 0 | 04-04-2002 11:11 AM |