Thread: Need a little assistance.

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    Need a little assistance.

    Create a C++ console application that allows two players to participate in a tic-tac-toe game. The program will be responsible for deciding who wins the game.

    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 &currentPlayer){
                 //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';
        }
    The output of the program should be similar to this:
    [ ] [ ] [ ]
    [ ] [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 &currentPlayer){
             //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';
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And what do you need help with?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #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.

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by drakenathaniel View Post
    Code:
    void TicTacToe::switchPlayer(char &currentPlayer){
             //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;
    
    }
    I suggest you stop programming while drunk
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    Great...

    Thanks I hope that works... ~sigh~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hello,i need assistance in completing the code
    By toader in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2009, 03:32 AM
  2. Variable Timer assistance
    By rich2852 in forum C Programming
    Replies: 2
    Last Post: 01-21-2009, 05:43 AM
  3. Any assistance would be greatly appreciated
    By iiwhitexb0iii in forum C Programming
    Replies: 18
    Last Post: 02-26-2006, 12:06 PM
  4. Need some more assistance
    By Thantos in forum Windows Programming
    Replies: 6
    Last Post: 08-14-2003, 12:13 PM
  5. Need assistance for major text base Arena RPG project
    By Ruflano in forum C++ Programming
    Replies: 0
    Last Post: 04-04-2002, 11:11 AM

Tags for this Thread