Rock-paper-scissors: Difference between revisions

(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( _moves, sizeof( _moves ) );
ZeroMemory( _win, sizeof( _win ) );
}
void draw() { _draw++; }
void win( int p ) { _win[p]++; }
void move( int p, int m ) { _moves[p][m]++; }
int getMove( int p, int m ) { return _moves[p][m]; }
void print()
{
char t[256];
wsprintf( t, "%.4d3d", _draw ); string d( t );
 
wsprintf( t, "%.4d3d", _win[PLAYER] ); string pw( t );
wsprintf( t, "%.4d3d", _moves[PLAYER][ROCK] ); string pr( t );
wsprintf( t, "%.4d3d", _moves[PLAYER][PAPER] ); string pp( t );
wsprintf( t, "%.4d3d", _moves[PLAYER][SCISSORS] ); string ps( t );
wsprintf( t, "%.3d", _moves[PLAYER][LIZARD] ); string pl( t );
 
wsprintf( t, "%.4d3d", _win_moves[COMPUTERPLAYER][SPOCK] ); string cwpk( t );
wsprintf( t, "%.4d", _moves[COMPUTER][ROCK] ); string cr( t );
wsprintf( t, "%.4d3d", _moves_win[COMPUTER][PAPER] ); string cpcw( t );
wsprintf( t, "%.4d3d", _moves[COMPUTER][SCISSORSROCK] ); string cscr( 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 << "+----------+----+---+--------+----------+----------+----------+--------+---------+" << endl;
cout << "| | WON | WON DRAW | DRAW ROCK | PAPER ROCK | SCISSORS | LIZARD PAPER| SPOCK | SCISSORS |" << endl;
cout << "+----------+----+---+--------+----------+----------+----------+--------+---------+" << endl;
cout << "| PLAYER | " << pw << " | PLAYER | " << pr << pw" | " << pp << " | " << ps << " | " << pl << " | " << prpk << " |" << endl;
cout << " |+----------+-------+ " << ppd << " | " << ps << " |+--------+---------+----------+--------+---------+" << 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;
 
Line 668 ⟶ 672:
 
private:
int _moves[2][3MX_C], _win[2], _draw;
};
//-------------------------------------------------------------------------------
Line 677 ⟶ 681:
{
int total = 0, r, s;
for( int i = 0; i < 3MX_C; total += statistics.getMove( PLAYER, 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;
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;
}
 
intvoid checkWinnerprintMove( int p, int m )
{
if( p == ROCKCOMPUTER ) 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 )
{
case 0ROCK: mscout =<< "ROCK\n"; break;
case 1PAPER: mscout =<< "PAPER\n"; break;
case 2SCISSORS: mscout =<< "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:
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)PAPERSPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? ";
cin >> p;
if( !p || p < 0 ) break;
if( p > 0 && p < 46 )
{
p--;
Line 752 ⟶ 746:
printMove( COMPUTER, m );
 
r = checkWinner( checker[p, ][m )];
switch( r )
{
case DRAW:
cout << endl << "DRAW!" << endl << endl;
statistics.draw();
 
break;
case COMPUTER:
cout << endl << "I WIN!" << endl << endl;
statistics.win( COMPUTER );
 
break;
case PLAYER:
cout << endl << "YOU WIN!" << endl << endl;
statistics.win( PLAYER );
 
}
system( "pause" );
}
system( "cls" );
}
statistics.print();
 
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
+--------------+----------+----------+----------+----------+----------+
 
| | 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>