Tic Tac Toe

Tic Tac Toe game

Page 1 of 1

8 Replies - 7679 Views - Last Post: 07 August 2010 - 01:22 AM Rate Topic: -----

#1 hardfrog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-July 10

Tic Tac Toe

Posted 05 August 2010 - 10:41 PM

Ok, I am having a problem with my code and keep getting these errors could someone help point me in the right direction:

Errors:
1>c:\users\administrator1\documents\visual studio 2010\projects\morris_lab1.ex1.cpp\morris_lab1.ex1.cpp\w6l6e2.1\w6l6e2.1\tic.cpp(89): warning C4258: 'k' : definition from the for loop is ignored; the definition from the enclosing scope is used
1> c:\users\administrator1\documents\visual studio 2010\projects\morris_lab1.ex1.cpp\morris_lab1.ex1.cpp\w6l6e2.1\w6l6e2.1\tic.cpp(87) : definition of 'k' ignored
1> c:\users\administrator1\documents\visual studio 2010\projects\morris_lab1.ex1.cpp\morris_lab1.ex1.cpp\w6l6e2.1\w6l6e2.1\tic.cpp(78) : definition of 'k' used
1>c:\users\administrator1\documents\visual studio 2010\projects\morris_lab1.ex1.cpp\morris_lab1.ex1.cpp\w6l6e2.1\w6l6e2.1\tic.cpp(104): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\administrator1\documents\visual studio 2010\projects\morris_lab1.ex1.cpp\morris_lab1.ex1.cpp\w6l6e2.1\w6l6e2.1\tic.cpp(104): error C2556: 'int TicTacToe::postMove(int,int,char)' : overloaded function differs only by return type from 'void TicTacToe::postMove(int,int,char)'
1> c:\users\administrator1\documents\visual studio 2010\projects\morris_lab1.ex1.cpp\morris_lab1.ex1.cpp\w6l6e2.1\w6l6e2.1\tic.cpp(15) : see declaration of 'TicTacToe::postMove'
1>c:\users\administrator1\documents\visual studio 2010\projects\morris_lab1.ex1.cpp\morris_lab1.ex1.cpp\w6l6e2.1\w6l6e2.1\tic.cpp(104): error C2371: 'TicTacToe::postMove' : redefinition; different basic types
1> c:\users\administrator1\documents\visual studio 2010\projects\morris_lab1.ex1.cpp\morris_lab1.ex1.cpp\w6l6e2.1\w6l6e2.1\tic.cpp(15) : see declaration of 'TicTacToe::postMove'


Here is my code:

#include <iostream>

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);
				void get_player1_move(int x1, int y1);  
				void get_player2_move(int x2, int y2);  
				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){
          //initialize the array contents
}
void TicTacToe::switchPlayer(char &currentPlayer)
{
         //switches the current player
	 if(currentPlayer == 'O')
	 {
		  currentPlayer ='X';
	 } 
	 else
	 {
		  currentPlayer = 'O';
	 }
}

void TicTacToe::showBoard()
{
	//display board game  
	cout<<"Tic-Tac-Toe Game"<<endl<<endl;  
	int array[3][3];
	int i =0;
	int x1=0;
	int x2 =0;
	int k =2;
	int y1 =0;
	int y2=0;
	for (int i=0; i<3; i++)  
	{
	for (int j=0; j<3; j++)  
    array[i][j]=0;  
	}
	{
	for (int k=0; k<3; k++)  
	cout<<array[k][0]<<" | "<<array[k][1]<<" | "<<array[k][2];  
	if (k!=2)  
	{
		cout<<"\n---------\n";
	}  
	cout<<endl<<endl;  
	cout<<"Enter your coordinates player1: ";  
	cin>>x1>>y1;  
	get_player1_move(x1,y1);  
	cout<<"\nEnter your coordinates player2: ";  
	cin>>x2>>y2;  
	get_player2_move(x2,y2);  
	cout<<endl;  
    }
}
void TicTacToe::postMove (int row, int col, char value)
{
       //gets the users move and posts it to the board
	 if (theBoard[row][col] == '-'){
		  theBoard[row][col] = value;
		  return true;
	 }
	 else{
		  cout << "Space is already taken, please choose a different one." << endl;
		  return false;
	 }
}

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



Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Tic Tac Toe

Posted 05 August 2010 - 10:49 PM

How have you gone about trying to solve the problem so far?

This post has been edited by Oler1s: 05 August 2010 - 10:50 PM

Was This Post Helpful? 0
  • +
  • -

#3 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Tic Tac Toe

Posted 05 August 2010 - 10:57 PM

Quote

warning C4258: 'k' : definition from the for loop is ignored; the definition from the enclosing scope is used


that's not some obscure error code defined in an un-documented obsolete header file somewhere!

Quote

\w6l6e2.1\w6l6e2.1\tic.cpp(104): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int


note that the (104) in the above error message indicates the LINE on which the error is, actually if you're using Visual Studio you can double click the error/warning message and it will take you to the source of the error.

This post has been edited by taylorc8: 05 August 2010 - 10:58 PM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_hardfrog*


Reputation:

Re: Tic Tac Toe

Posted 05 August 2010 - 11:16 PM

View PostOler1s, on 05 August 2010 - 09:49 PM, said:

How have you gone about trying to solve the problem so far?



My first time running code I had several errors after looking over my code I relized that I forgot to int the x1 and such. Tried to do the same with K but that did not solve my problem, I am lost. I know it has to be something simple but this is my first time working with classes in C++. I am not looking for the answer, just some guideance on where to start other than the errors. Like a piece of useful information that would help me determine the solution, not the complete answer just a hint.
Was This Post Helpful? 0

#5 Guest_hardfrog*


Reputation:

Re: Tic Tac Toe

Posted 05 August 2010 - 11:25 PM

Ok, I have edited my code to this but still have these error messages:

#include <iostream>

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);
				void get_player1_move(int x1, int y1);  
				void get_player2_move(int x2, int y2);  
				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){
          //initialize the array contents
}
void TicTacToe::switchPlayer(char &currentPlayer)
{
         //switches the current player
	 if(currentPlayer == 'O')
	 {
		  currentPlayer ='X';
	 } 
	 else
	 {
		  currentPlayer = 'O';
	 }
}

void TicTacToe::showBoard()
{
	//display board game  
	cout<<"Tic-Tac-Toe Game"<<endl<<endl;  
	int array[3][3];
	int i =0;
	int x1=0;
	int x2 =0;
	int k =2;
	int y1 =0;
	int y2=0;
	for (int i=0; i<3; i++)  
	{
	for (int j=0; j<3; j++)  
    array[i][j]=0;  
	}
	{
	for (int k=0; k<3; k++)  
	cout<<array[k][0]<<" | "<<array[k][1]<<" | "<<array[k][2];  
	if (k!=2)  
	{
		cout<<"\n---------\n";
	}  
	cout<<endl<<endl;  
	cout<<"Enter your coordinates player1: ";  
	cin>>x1>>y1;  
	get_player1_move(x1,y1);  
	cout<<"\nEnter your coordinates player2: ";  
	cin>>x2>>y2;  
	get_player2_move(x2,y2);  
	cout<<endl;  
    }
}
void TicTacToe::postMove (int row, int col, char value)
{
       //gets the users move and posts it to the board
	 if (theBoard[row][col] == '-'){
		  theBoard[row][col] = value;
		  return true;
	 }
	 else{
		  cout << "Space is already taken, please choose a different one." << endl;
		  return false;
	 }
}

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



Error messages:
>c:\users\administrator1\documents\visual studio 2010\projects\morris_lab1.ex1.cpp\morris_lab1.ex1.cpp\w6l6e2.1\w6l6e2.1\tic.cpp(89): warning C4258: 'k' : definition from the for loop is ignored; the definition from the enclosing scope is used
1> c:\users\administrator1\documents\visual studio 2010\projects\morris_lab1.ex1.cpp\morris_lab1.ex1.cpp\w6l6e2.1\w6l6e2.1\tic.cpp(87) : definition of 'k' ignored
1> c:\users\administrator1\documents\visual studio 2010\projects\morris_lab1.ex1.cpp\morris_lab1.ex1.cpp\w6l6e2.1\w6l6e2.1\tic.cpp(78) : definition of 'k' used
1>c:\users\administrator1\documents\visual studio 2010\projects\morris_lab1.ex1.cpp\morris_lab1.ex1.cpp\w6l6e2.1\w6l6e2.1\tic.cpp(108): error C2562: 'TicTacToe::postMove' : 'void' function returning a value
1> c:\users\administrator1\documents\visual studio 2010\projects\morris_lab1.ex1.cpp\morris_lab1.ex1.cpp\w6l6e2.1\w6l6e2.1\tic.cpp(15) : see declaration of 'TicTacToe::postMove'
1>c:\users\administrator1\documents\visual studio 2010\projects\morris_lab1.ex1.cpp\morris_lab1.ex1.cpp\w6l6e2.1\w6l6e2.1\tic.cpp(112): error C2562: 'TicTacToe::postMove' : 'void' function returning a value
1> c:\users\administrator1\documents\visual studio 2010\projects\morris_lab1.ex1.cpp\morris_lab1.ex1.cpp\w6l6e2.1\w6l6e2.1\tic.cpp(15) : see declaration of 'TicTacToe::postMove'
Was This Post Helpful? 0

#6 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5940
  • View blog
  • Posts: 12,868
  • Joined: 16-October 07

Re: Tic Tac Toe

Posted 06 August 2010 - 05:29 AM

So, you were given this for homework:
http://www.cplusplus.../beginner/9849/
http://studentoffort...ensional-Array-

You've done what, exactly?
Was This Post Helpful? 2
  • +
  • -

#7 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Tic Tac Toe

Posted 06 August 2010 - 02:11 PM

If you don't know anything about debugging, scroll to the very FIRST error message the compiler gives you, and correct that one first. Errors following it can be caused by that error.

hope that helps.
Was This Post Helpful? 2
  • +
  • -

#8 jaia  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 61
  • Joined: 21-March 10

Re: Tic Tac Toe

Posted 06 August 2010 - 09:31 PM

The key word in that error message is "scope". If you haven't learned about scoping rules yet, find that section in your textbook and read it. Pay particular attention to the scope of index variables in loops.
Was This Post Helpful? 1
  • +
  • -

#9 Eclipse Reborn  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 135
  • Joined: 26-June 10

Re: Tic Tac Toe

Posted 07 August 2010 - 01:22 AM

No offense, but by saying "point me in the right direction", translates into "what do I do to fix the error?".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1