Anonymous user
Rock-paper-scissors: Difference between revisions
→{{header|C++}}
(Shorter second D entry) |
|||
Line 610:
=={{header|C++}}==
Version using Additional Weapons
<lang cpp>
#include <windows.h>
Line 619 ⟶ 621:
//-------------------------------------------------------------------------------
enum choices { ROCK, SPOCK, PAPER, LIZARD, SCISSORS, MX_C };
enum indexes { PLAYER, COMPUTER, DRAW };
Line 628 ⟶ 630:
stats() : _draw( 0 )
{
ZeroMemory( _win, sizeof( _win ) );
}
void draw()
void win( int p )
void move( int p, int m ) { _moves[p][m]++; }
int getMove( int p, int m ) { return _moves[p][m]; }
void print()
{
wsprintf( t, "%.
wsprintf( t, "%.
wsprintf( t, "%.
wsprintf( t, "%.
wsprintf( t, "%.
wsprintf( t, "%.3d", _moves[PLAYER][LIZARD] ); string pl( t );
wsprintf( t, "%.
wsprintf( t, "%.
wsprintf( t, "%.
wsprintf( t, "%.3d", _moves[COMPUTER][PAPER] ); string cp( t );
wsprintf( t, "%.3d", _moves[COMPUTER][SCISSORS] ); string cs( t );
wsprintf( t, "%.3d", _moves[COMPUTER][LIZARD] ); string cl( t );
wsprintf( t, "%.3d", _moves[COMPUTER][SPOCK] ); string ck( t );
system( "cls" );
cout << endl;
cout << "+----------+----
cout << "| | WON |
cout << "+----------+----
cout << "| PLAYER | " << pw << " |
cout << "
cout << "| COMPUTER | " << cw << " | | " << cr << " | " << cp << " | " << cs << " | " << cl << " | " << ck << " |" << endl;
cout << "+----------+-------+--------+--------+---------+----------+--------+---------+" << endl;
cout << endl << endl;
Line 668 ⟶ 672:
private:
int _moves[2][
};
//-------------------------------------------------------------------------------
Line 677 ⟶ 681:
{
int total = 0, r, s;
for( int i = 0; i <
r = rand() % total;
s = statistics.getMove( PLAYER, ROCK );
if( r < s ) return SPOCK;
r -= s;
s = statistics.getMove( PLAYER, SPOCK );
if( r < s ) return PAPER;
r -= s;
s = statistics.getMove( PLAYER, PAPER );
if( r < s ) return LIZARD;
r -= s;
s = statistics.getMove( PLAYER, LIZARD );
if( r < s ) return SCISSORS;
r -= s;
return ROCK;
}
{
if( p ==
else cout << "Your move: ";
switch( m )
{
case
case
case
case LIZARD: cout << "LIZARD\n"; break;
case SPOCK: cout << "SPOCK\n";
}
}
public:
rps()
{
checker[ROCK][ROCK] = 2; checker[ROCK][PAPER] = 1; checker[ROCK][SCISSORS] = 0; checker[ROCK][LIZARD] = 0; checker[ROCK][SPOCK] = 1;
checker[PAPER][ROCK] = 0; checker[PAPER][PAPER] = 2; checker[PAPER][SCISSORS] = 1; checker[PAPER][LIZARD] = 1; checker[PAPER][SPOCK] = 0;
checker[SCISSORS][ROCK] = 1; checker[SCISSORS][PAPER] = 0; checker[SCISSORS][SCISSORS] = 2; checker[SCISSORS][LIZARD] = 0; checker[SCISSORS][SPOCK] = 1;
checker[LIZARD][ROCK] = 1; checker[LIZARD][PAPER] = 0; checker[LIZARD][SCISSORS] = 1; checker[LIZARD][LIZARD] = 2; checker[LIZARD][SPOCK] = 0;
checker[SPOCK][ROCK] = 0; checker[SPOCK][PAPER] = 1; checker[SPOCK][SCISSORS] = 0; checker[SPOCK][LIZARD] = 1; checker[SPOCK][SPOCK] = 2;
}
void play()
{
Line 738 ⟶ 732:
while( true )
{
cout << "What is your move (1)ROCK (2)
cin >> p;
if( !p || p < 0 ) break;
if( p > 0 && p <
{
p--;
Line 752 ⟶ 746:
printMove( COMPUTER, m );
r =
switch( r )
{
case DRAW:
break;
case COMPUTER:
cout << endl << "I WIN!" << endl << endl;
statistics.win( COMPUTER );
break;
case PLAYER:
}
}
system( "cls" );
}
statistics.print();
}
private:
stats statistics;
int checker[MX_C][MX_C];
};
//-------------------------------------------------------------------------------
Line 794 ⟶ 786:
Sample output:
<pre>
What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? 1
Your move: ROCK
My move: PAPER
I WIN!
What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? 3
Your move: PAPER
My move: SPOCK
YOU WIN!
What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? 1
Your move: SPOCK
My move: LIZARD
I WIN!
[...]
+----------+-------+--------+--------+---------+----------+--------+---------+
| | WON | DRAW | ROCK | PAPER | SCISSORS | LIZARD | SPOCK |
+----------+-------+--------+--------+---------+----------+--------+---------+
| PLAYER | 001 | | 003 | 002 | 000 | 000 | 009 |
+----------+-------+ 005 +--------+---------+----------+--------+---------+
| COMPUTER | 008 | | 000 | 005 | 000 | 001 | 008 |
+----------+-------+--------+--------+---------+----------+--------+---------+
</pre>
|