Rock-paper-scissors: Difference between revisions

Content added Content deleted
(Shorter second D entry)
Line 610: Line 610:


=={{header|C++}}==
=={{header|C++}}==
Version using Additional Weapons

<lang cpp>
<lang cpp>
#include <windows.h>
#include <windows.h>
Line 619: Line 621:


//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
enum choices { ROCK, PAPER, SCISSORS };
enum choices { ROCK, SPOCK, PAPER, LIZARD, SCISSORS, MX_C };
enum indexes { PLAYER, COMPUTER, DRAW };
enum indexes { PLAYER, COMPUTER, DRAW };


Line 628: Line 630:
stats() : _draw( 0 )
stats() : _draw( 0 )
{
{
ZeroMemory( _moves, sizeof( _moves ) );
ZeroMemory( _moves, sizeof( _moves ) );
ZeroMemory( _win, sizeof( _win ) );
ZeroMemory( _win, sizeof( _win ) );
}
}
void draw() { _draw++; }
void draw() { _draw++; }
void win( int p ) { _win[p]++; }
void win( int p ) { _win[p]++; }
void move( int p, int m ) { _moves[p][m]++; }
void move( int p, int m ) { _moves[p][m]++; }
int getMove( int p, int m ) { return _moves[p][m]; }
int getMove( int p, int m ) { return _moves[p][m]; }
void print()
void print()
{
{
char t[256];
char t[256];
wsprintf( t, "%.4d", _draw ); string d( t );
wsprintf( t, "%.3d", _draw ); string d( t );


wsprintf( t, "%.4d", _win[PLAYER] ); string pw( t );
wsprintf( t, "%.3d", _win[PLAYER] ); string pw( t );
wsprintf( t, "%.4d", _moves[PLAYER][ROCK] ); string pr( t );
wsprintf( t, "%.3d", _moves[PLAYER][ROCK] ); string pr( t );
wsprintf( t, "%.4d", _moves[PLAYER][PAPER] ); string pp( t );
wsprintf( t, "%.3d", _moves[PLAYER][PAPER] ); string pp( t );
wsprintf( t, "%.4d", _moves[PLAYER][SCISSORS] ); string ps( t );
wsprintf( t, "%.3d", _moves[PLAYER][SCISSORS] ); string ps( t );
wsprintf( t, "%.3d", _moves[PLAYER][LIZARD] ); string pl( t );

wsprintf( t, "%.4d", _win[COMPUTER] ); string cw( t );
wsprintf( t, "%.3d", _moves[PLAYER][SPOCK] ); string pk( t );
wsprintf( t, "%.4d", _moves[COMPUTER][ROCK] ); string cr( t );
wsprintf( t, "%.4d", _moves[COMPUTER][PAPER] ); string cp( t );
wsprintf( t, "%.3d", _win[COMPUTER] ); string cw( t );
wsprintf( t, "%.4d", _moves[COMPUTER][SCISSORS] ); string cs( t );
wsprintf( t, "%.3d", _moves[COMPUTER][ROCK] ); string cr( 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" );
system( "cls" );
cout << endl;
cout << endl;
cout << "+--------------+----------+----------+----------+----------+----------+" << endl;
cout << "+----------+-------+--------+--------+---------+----------+--------+---------+" << endl;
cout << "| | WON | DRAW | ROCK | PAPER | SCISSORS |" << endl;
cout << "| | WON | DRAW | ROCK | PAPER | SCISSORS | LIZARD | SPOCK |" << endl;
cout << "+--------------+----------+----------+----------+----------+----------+" << endl;
cout << "+----------+-------+--------+--------+---------+----------+--------+---------+" << endl;
cout << "| PLAYER | " << pw << " | | " << pr;
cout << "| PLAYER | " << pw << " | | " << pr << " | " << pp << " | " << ps << " | " << pl << " | " << pk << " |" << endl;
cout << " | " << pp << " | " << ps << " |" << endl;
cout << "+----------+-------+ " << d << " +--------+---------+----------+--------+---------+" << endl;
cout << "| COMPUTER | " << cw << " | | " << cr << " | " << cp << " | " << cs << " | " << cl << " | " << ck << " |" << endl;
cout << "+--------------+----------+ " << d << " +----------+----------+----------+" << endl;
cout << "+----------+-------+--------+--------+---------+----------+--------+---------+" << endl;
cout << "| COMPUTER | " << cw << " | | " << cr;
cout << " | " << cp << " | " << cs << " |" << endl;
cout << "+--------------+----------+----------+----------+----------+----------+" << endl;
cout << endl << endl;
cout << endl << endl;


Line 668: Line 672:


private:
private:
int _moves[2][3], _win[2], _draw;
int _moves[2][MX_C], _win[2], _draw;
};
};
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
Line 677: Line 681:
{
{
int total = 0, r, s;
int total = 0, r, s;
for( int i = 0; i < 3; total += statistics.getMove( PLAYER, i++ ) );
for( int i = 0; i < MX_C; total += statistics.getMove( PLAYER, i++ ) );
r = rand() % total;
r = rand() % total;

s = statistics.getMove( PLAYER, ROCK );
s = statistics.getMove( PLAYER, ROCK );
if( r < s ) return SPOCK;
r -= s;

s = statistics.getMove( PLAYER, SPOCK );
if( r < s ) return PAPER;
if( r < s ) return PAPER;
r -= s;
if( r - s < statistics.getMove( PLAYER, PAPER ) ) return SCISSORS;

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;
return ROCK;
}
}


int checkWinner( int p, int m )
void printMove( int p, int m )
{
{
if( p == ROCK )
if( p == COMPUTER ) cout << "My move: ";
else cout << "Your move: ";
{
switch( m )
{
case PAPER: return COMPUTER;
case SCISSORS: return PLAYER;
default: return DRAW;
}
}
if( p == PAPER )
{
switch( m )
{
case SCISSORS: return COMPUTER;
case ROCK: return PLAYER;
default: return DRAW;
}
}
if( p == SCISSORS )
{
switch( m )
{
case ROCK: return COMPUTER;
case PAPER: return PLAYER;
default: return DRAW;
}
}
return DRAW;
}


void printMove( int p, int m )
{
string ms = "";
switch( m )
switch( m )
{
{
case 0: ms = "ROCK"; break;
case ROCK: cout << "ROCK\n"; break;
case 1: ms = "PAPER"; break;
case PAPER: cout << "PAPER\n"; break;
case 2: ms = "SCISSORS"; break;
case SCISSORS: cout << "SCISSORS\n"; break;
case LIZARD: cout << "LIZARD\n"; break;
case SPOCK: cout << "SPOCK\n";
}
}

if( p == COMPUTER ) cout << "My move: ";
else cout << "Your move: ";
cout << ms << endl;
}
}


public:
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()
void play()
{
{
Line 738: Line 732:
while( true )
while( true )
{
{
cout << "What is your move (1)ROCK (2)PAPER (3)SCISSORS (0)Quit ? ";
cout << "What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? ";
cin >> p;
cin >> p;
if( !p || p < 0 ) break;
if( !p || p < 0 ) break;
if( p > 0 && p < 4 )
if( p > 0 && p < 6 )
{
{
p--;
p--;
Line 752: Line 746:
printMove( COMPUTER, m );
printMove( COMPUTER, m );


r = checkWinner( p, m );
r = checker[p][m];
switch( r )
switch( r )
{
{
case DRAW:
case DRAW:
cout << endl << "DRAW!" << endl << endl;
cout << endl << "DRAW!" << endl << endl;
statistics.draw();
statistics.draw();

break;
break;
case COMPUTER:
case COMPUTER:
cout << endl << "I WIN!" << endl << endl;
cout << endl << "I WIN!" << endl << endl;
statistics.win( COMPUTER );
statistics.win( COMPUTER );

break;
break;
case PLAYER:
case PLAYER:
cout << endl << "YOU WIN!" << endl << endl;
cout << endl << "YOU WIN!" << endl << endl;
statistics.win( PLAYER );
statistics.win( PLAYER );


}
}
system( "pause" );
system( "pause" );
}
}
system( "cls" );
system( "cls" );
}
}
statistics.print();

statistics.print();
}
}


private:
private:
stats statistics;
stats statistics;
int checker[MX_C][MX_C];
};
};
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
Line 794: Line 786:
Sample output:
Sample output:
<pre>
<pre>
What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? 1
+--------------+----------+----------+----------+----------+----------+

| | WON | DRAW | ROCK | PAPER | SCISSORS |
Your move: ROCK
+--------------+----------+----------+----------+----------+----------+
My move: PAPER
| PLAYER | 0003 | | 0006 | 0004 | 0002 |

+--------------+----------+ 0001 +----------+----------+----------+
I WIN!
| COMPUTER | 0008 | | 0001 | 0003 | 0008 |

+--------------+----------+----------+----------+----------+----------+
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>
</pre>