Math Game

Trying to make a Math Game Called “22” by programming. Two players will be able to play the game at a time. The rule of the game is, one has to AVOID making the addition of all the numbers inputted be 22. One player can put only one number at a time out of the numbers 1,2,3. The inputted number of both the players will be added. Basically, it’s a little strategic and guessing game and the person whose last inputted number makes the addition equal to 22 or above, LOSES.

the starting player can always win by following strategy!
players: A and B. A inputs first (& win)

A1: 1,
B1: 1 or 2 or 3
A2: number to sum up 5.
B2: 1 or 2 or 3
A3: number to sum up 9.
B3: 1 or 2 or 3
A4: number to sum up 13.
B4: 1 or 2 or 3
A5: number to sum up 17.
B5: 1 or 2 or 3
A6: number to sum up 21.
B6: 1 or 2 or 3 (sum>=22, B loses)
@anup30 that's not the issue here... ^^

@Armaan, what have you tried so far? :)
> that's not the issue here...

i wasn't taking about a issue.
just posted my observation.
because its the logic behind the strongest computer opponent of that game!
@Gamer2015.

Frankly speaking. In a way, i don't have any idea. That's why i asked if someone could solve it.
> that's not the issue here...

i wasn't taking about a issue.
just posted my observation.
because its the logic behind the strongest computer opponent of that game!

okey

@Gamer2015.

Frankly speaking. In a way, i don't have any idea. That's why i asked if someone could solve it.

So... you want us to do your homework?
This will not happen :)
Try some things that come to your mind and post your code here, we'll then help you to solve it yourself.
We want to help you to learn and get a better understanding of C++ and programming in general, we don't want to make a programm for you which should help you learn more.

I'll be so nice and help you to start with your programm ;)
1
2
3
4
5
6
7
8
9
10
11
int main(void)
{
    int number = 0;

    while(number < 22)
    {
        
    }
    
    return 0;
}
@OP, the two opponents can be three types:

1. human vs human (in this case program will just count the score and declare the winner.)

2. human vs program (what will be the program logic: random guess? or logic as i described above?)

3. program vs program

which version you are trying?
human v/s human
I'd strongly suggest you think through this program, step by step, and type out what the program would have to do in each step (This is known as pseudo code); from there, the community is in a much better position to help you transform those thoughts into code. For instance, Gamer2015 has you off to a good start, but lets keep it simple and stick to English and then concentrate on transforming that English into C++.

For example, I can type out some of the basic steps involved in the Card Game: Go Fish
1
2
3
4
5
6
7
8
9
1) Players A and B are dealt a hand of cards.
2) Player A asks if Player B has Ace - King (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13).
3) If Player B has Player A's specified card . . .
    4) Player B hands player A the specified card.
    5) Player A discards the constructed pair.
    6) Player A gets another turn.
7) Else . . .
    8) Player B says, "Go Fish" and player A draws a card.
    9) Player A ends his turn. 


Notice, I used no C++ whatsoever. I simply wrote out what needed to happen for a turn in "Go Fish" to be considered complete. While this small fragment of pseudocode may not cover all of the fine coding nuances, it helps to create a starting point from which I can further elaborate. Similarly, you can do this with the program you're trying to write and we can help you elaborate and transform those steps into code.

If you happen to come up with these steps, post them here. Hope this helps.
Last edited on
>That's why i asked if someone could solve it.
> human v/s human

ok.
here is a solution using mutual recursion.
you can try it using nested loop (easier).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;

void play22();
int pl_1_play(const string &pl_1, const string &pl_2, int count=0);
int pl_2_play(const string &pl_1, const string &pl_2, int count);


int main(){		
	play22();
	return 0;
}


void play22(){
	cout << "Welcome to TwentyTwo Game: \n";
	string pl_1, pl_2;	
	cout << "Name of player1 : ";
	getline(cin, pl_1);
	cout << "Name of player2 : ";
	getline(cin, pl_2);	
	pl_1_play(pl_1, pl_2);
}


int pl_1_play(const string &pl_1, const string &pl_2, int count){
	int i;
	cout << pl_1 << ", enter number (1,2,3) ";
	cin >> i;
	if(i!=1 && i!=2 && i!=3){
		cout << "wrong input. try again.\n";
		pl_1_play(pl_1, pl_2, count);
	}
	else{
		count += i;
		cout << "Sum = " << count << "\n";
		if(count<22) {
			int val = pl_2_play(pl_1, pl_2, count);
			if(val==1) return 0;
		}
		else{
			cout << "Sum greater or equal to 22, " << pl_2 << " wins.\n";
			return 0;
		}				
	}
}


int pl_2_play(const string &pl_1, const string &pl_2, int count){
	int i;
	cout << pl_2 << ", enter number (1,2,3) ";
	cin >> i;
	if(i!=1 && i!=2 && i!=3){
		cout << "wrong input. try again.\n";
		pl_2_play(pl_1, pl_2, count);
	}
	else{
		count+=i;
		cout << "Sum = " << count << "\n";
		if(count<22) pl_1_play(pl_1, pl_2, count);
		else{
			cout << "Sum greater or equal to 22, " << pl_1 << " wins.\n";
			return 1;
		}		
	}
}
Topic archived. No new replies allowed.