954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

tic-tac-toe numbers version help.

can anybody help me making this program?
i've based my game from this site. http://www.education.com/activity/article/Tick_tack_toe_Added/

hope that someone can help me..

i've been stuck coding that for almost 6hrs but still no progress.

thank you!

jankeifer
Newbie Poster
12 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Yes we can. Problem is we're not all psychic so
1) we can't help you fix the code we can not see, nor
2) we can't answer questions that were not asked.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
Yes we can. Problem is we're not all psychic so 1) we can't help you fix the code we can not see, nor 2) we can't answer questions that were not asked.


uhm.. can you please give me an idea on how the main code will be coded?

jankeifer
Newbie Poster
12 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

here's my code so far.

#include <iostream>
#include <map>
using namespace std;

int main(){

map<char,char> box;

box['a'] = 'a';
box['b'] = 'b';
box['c'] = 'c';
box['d'] = 'd';
box['e'] = 'e';
box['f'] = 'f';
box['g'] = 'g';
box['h'] = 'h';
box['i'] = 'i';

char letter,value;

cout << endl << box['a']<<" | "<<box['b']<<" | "<<box['c'];
cout << "\n---------\n";
cout << box['d']<<" | "<<box['e']<<" | "<<box['f'];
cout << "\n---------\n";
cout << box['g']<<" | "<<box['h']<<" | "<<box['i'];

cout<<endl<<"\nEnter a letter: ";
cin>>letter;
cout<<endl<<"Enter a value: ";
cin>>value;
box[letter] = value;
cout << endl << box['a']<<" | "<<box['b']<<" | "<<box['c'];
cout << "\n---------\n";
cout << box['d']<<" | "<<box['e']<<" | "<<box['f'];
cout << "\n---------\n";
cout << box['g']<<" | "<<box['h']<<" | "<<box['i']<< endl;



system ("pause");
}
jankeifer
Newbie Poster
12 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Other than no formatting making it hard to read, that's seems to be a good start. (click the link)

Now continue adding more functionality.
- Maybe put the board display in a function so you only need it once.
- Maybe put the game in a loop so you don't need 9 individual inputs.
- You could even test to see it a chosen position is already taken and display an error.

And if you need more help, please refer to my previous post

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

i'm just having a hard time in my algorithm.. i'm getting confused. @.@

jankeifer
Newbie Poster
12 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Yeah? And how does that help in our understanding of your problem? Please stop ignoring my links.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
#include <iostream>
#include <iomanip>
#include <map>
#include <time.h>
#include <cstdlib>
using namespace std;

//displays the instruction of the game
void displayInst(){
    for ( int b = 0; b < 28; b++ )
    cout << "* ";

    cout << "\n* Game Title: NUM-BERS                                 *";
    cout << "\n*                                                      *";
    cout << "\n* Players : 2                                          *";
    cout << "\n*                                                      *";
    cout << "\n* How-to-play: The game is basically                   *";
    cout << "\n*              based on \"tic-tac-toe\" but with a       *";
    cout << "\n*              twist. Instead of Using X's and O's     *";
    cout << "\n*              the player is going to use numbers,     *";
    cout << "\n*              ODD for PLAYER 1 and EVEN for PLAYER 2. *";
    cout << "\n*              The goal is to make a \"in a row\";       *";
    cout << "\n*              (diagonally, vertically,horizontally)   *";
    cout << "\n*              but the catch is, the total of the row  *";
    cout << "\n*              will be equal to the \"goal total\"       *";
    cout << "\n*              generated by the program.               *\n";
    for ( int b = 0; b < 28; b++ )
        cout << "* ";
}//end displayInst


int main(){
    
   char choice;     
   
   do{
        
        map<char,char> box;
        box['a'] = 'a';
        box['b'] = 'b';
        box['c'] = 'c';
        box['d'] = 'd';
        box['e'] = 'e';
        box['f'] = 'f';
        box['g'] = 'g';
        box['h'] = 'h';
        box['i'] = 'i';

        char letter,value;   
        int goalTotal;
    
        //generates a random number for the total goal
        srand( time(NULL) );
        goalTotal = (5 + rand() % 15);
      	int a[8];
      	
      	
        do{
		
            displayInst();//displays the instructions
        
            //display's the main board
            cout << endl << endl << setw(6) << box['a']<<" | "<<box['b']<<" | "<<box['c'];
            cout << "\n     ---------\n";
            cout << setw(6) << box['d'] << " | " << box['e'] << " | " << box['f'];
            cout << "\n     ---------\n";
            cout << setw(6) << box['g'] << " | " << box['h'] << " | " << box['i'] << endl;
        
		    cout << setw(6) << "\nGoal Total to Win: " << goalTotal;//displays the total goal for the game
            //lets the user input their move
            cout << endl << setw(6) << "\nEnter a letter: ";
            cin >> letter;
            cout << endl << setw(6) << "Enter a value: ";
            cin >> value;
       
            box[letter] = value;
        
		   
    		//check if player wins

            a[0] = int(box['a'] - 48) + int(box['b'] - 48)  + int(box['c'] - 48);
            a[1] = int(box['d'] - 48) + int(box['e'] - 48)  + int(box['f'] - 48);
    		a[2] = int(box['g'] - 48) + int(box['h'] - 48)  + int(box['i'] - 48);
    	  	a[3] = int(box['a'] - 48) + int(box['d'] - 48)  + int(box['g'] - 48);
    		a[4] = int(box['b'] - 48) + int(box['e'] - 48)  + int(box['h'] - 48);
    		a[5] = int(box['c'] - 48) + int(box['f'] - 48)  + int(box['i'] - 48);
    		a[6] = int(box['g'] - 48) + int(box['e'] - 48)  + int(box['c'] - 48);
    		a[7] = int(box['a'] - 48) + int(box['e'] - 48)  + int(box['i'] - 48);
		
    		if(a[0] == goalTotal) cout << "\nPlayer Wins\n";
    		if(a[1] == goalTotal) cout << "\nPlayer Wins\n";
    		if(a[2] == goalTotal) cout << "\nPlayer Wins\n";
    		if(a[3] == goalTotal) cout << "\nPlayer Wins\n";
    		if(a[4] == goalTotal) cout << "\nPlayer Wins\n";
    		if(a[5] == goalTotal) cout << "\nPlayer Wins\n";
    		if(a[6] == goalTotal) cout << "\nPlayer Wins\n";
    		if(a[7] == goalTotal) cout << "\nPlayer Wins\n";

        
       }while(a[0] != goalTotal || a[1]  == goalTotal || a[2]  != goalTotal || a[3]  != goalTotal || a[4]  != goalTotal || a[5]  != goalTotal || a[6] != goalTotal || a[7]  != goalTotal);

	cout << "Play again? (Y/N): ";
	cin >> choice;
	
	}while( toupper(choice) == 'Y');

return 0;
}


guys! can you please help on what do i have to do to end my program?
after the player wins it still continues to play the game..
i know its in the do while..
but please

can someone modify it?
thanks!

jankeifer
Newbie Poster
12 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Since you are already testing for wins, we can use that to simplify the loop exit. First we'll add a Boolean variable named win and initialize it to false:

bool win = false;


Then we'll simplify the test for winning:

for (int i = 0; i < 8; i++) {
                      if (a[i] == goalTotal) {
                          cout << endl << "Player Wins" << endl;
                          win = true;


Finally, we change the conditional of the do/while() loop:

while(!win);


Try that and see if it works.

Schol-R-LEA
Posting Pro
556 posts since Oct 2010
Reputation Points: 254
Solved Threads: 85
 

Since you are already testing for wins, we can use that to simplify the loop exit. First we'll add a Boolean variable named win and initialize it to false:

bool win = false;

Then we'll simplify the test for winning:

for (int i = 0; i < 8; i++) {
                      if (a[i] == goalTotal) {
                          cout << endl << "Player Wins" << endl;
                          win = true;

Finally, we change the conditional of the do/while() loop:

while(!win);

Try that and see if it works.


is this correct?

#include <iostream>
#include <iomanip>
#include <map>
#include <time.h>
#include <cstdlib>
using namespace std;

//displays the instruction of the game
void displayInst(){
    for ( int b = 0; b < 28; b++ )
    cout << "* ";

    cout << "\n* Game Title: NUM-BERS                                 *";
    cout << "\n*                                                      *";
    cout << "\n* Players : 2                                          *";
    cout << "\n*                                                      *";
    cout << "\n* How-to-play: The game is basically                   *";
    cout << "\n*              based on \"tic-tac-toe\" but with a       *";
    cout << "\n*              twist. Instead of Using X's and O's     *";
    cout << "\n*              the player is going to use numbers,     *";
    cout << "\n*              ODD for PLAYER 1 and EVEN for PLAYER 2. *";
    cout << "\n*              The goal is to make a \"in a row\";       *";
    cout << "\n*              (diagonally, vertically,horizontally)   *";
    cout << "\n*              but the catch is, the total of the row  *";
    cout << "\n*              will be equal to the \"goal total\"       *";
    cout << "\n*              generated by the program.               *\n";
    for ( int b = 0; b < 28; b++ )
        cout << "* ";
}//end displayInst


int main(){
    
   char choice;     
   
   do{
        
        map<char,char> box;
        box['a'] = 'a';
        box['b'] = 'b';
        box['c'] = 'c';
        box['d'] = 'd';
        box['e'] = 'e';
        box['f'] = 'f';
        box['g'] = 'g';
        box['h'] = 'h';
        box['i'] = 'i';

        char letter,value;   
        int goalTotal;
    
        //generates a random number for the total goal
        srand( time(NULL) );
        goalTotal = (5 + rand() % 15);
      	int a[8];
      	bool win = false;
      	
        do{
		
            displayInst();//displays the instructions
        
            //display's the main board
            cout << endl << endl << setw(6) << box['a']<<" | "<<box['b']<<" | "<<box['c'];
            cout << "\n     ---------\n";
            cout << setw(6) << box['d'] << " | " << box['e'] << " | " << box['f'];
            cout << "\n     ---------\n";
            cout << setw(6) << box['g'] << " | " << box['h'] << " | " << box['i'] << endl;
        
		    cout << setw(6) << "\nGoal Total to Win: " << goalTotal;//displays the total goal for the game
            //lets the user input their move
            cout << endl << setw(6) << "\nEnter a letter: ";
            cin >> letter;
            cout << endl << setw(6) << "Enter a value: ";
            cin >> value;
       
            box[letter] = value;
        
		   
    		//check if player wins

            a[0] = int(box['a'] - 48) + int(box['b'] - 48)  + int(box['c'] - 48);
            a[1] = int(box['d'] - 48) + int(box['e'] - 48)  + int(box['f'] - 48);
    		a[2] = int(box['g'] - 48) + int(box['h'] - 48)  + int(box['i'] - 48);
    	  	a[3] = int(box['a'] - 48) + int(box['d'] - 48)  + int(box['g'] - 48);
    		a[4] = int(box['b'] - 48) + int(box['e'] - 48)  + int(box['h'] - 48);
    		a[5] = int(box['c'] - 48) + int(box['f'] - 48)  + int(box['i'] - 48);
    		a[6] = int(box['g'] - 48) + int(box['e'] - 48)  + int(box['c'] - 48);
    		a[7] = int(box['a'] - 48) + int(box['e'] - 48)  + int(box['i'] - 48);
		
            for (int i = 0; i < 8; i++) {
              if (a[i] == goalTotal) 
                cout << endl << "Player Wins" << endl;
               }  win = true;
             
        } while(!win);
     

	cout << "Play again? (Y/N): ";
	cin >> choice;
	
	}while( toupper(choice) == 'Y');

return 0;
}


the program ends but it doesn't finish the game

jankeifer
Newbie Poster
12 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Sorry, that was my mistake - I failed to close the two open brackets. It should go as follows:

for (int i = 0; i < 8; i++) {
                      if (a[i] == goalTotal) {
                          cout << endl << "Player Wins" << endl;
                          win = true;
                          break;
                      }
                  }
Schol-R-LEA
Posting Pro
556 posts since Oct 2010
Reputation Points: 254
Solved Threads: 85
 

thank you so much.. gonna try it now.. :))

jankeifer
Newbie Poster
12 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Sorry, that was my mistake - I failed to close the two open brackets. It should go as follows:

for (int i = 0; i < 8; i++) {
                      if (a[i] == goalTotal) {
                          cout << endl << "Player Wins" << endl;
                          win = true;
                          break;
                      }
                  }


master Schoil-R-LEA,
how can i make it into 2 players..
i mean, how can i display on who's turn is it to play?

if it's not much of a burden.. can you help me? :)

jankeifer
Newbie Poster
12 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: