Game of Paper, Rock and scissors

Paper-Scissors-Rock is a game for two players. Each player simultaneously opens his/her hand to display a symbol:
* Fist equals rock,
* Open hand equals paper,
* Showing the index and middle finger equals scissors.

The winner is determined by the following schema:
* paper beats (wraps) rock,
* rock beats (blunts) scissors,
* scissors beats (cuts) paper.

Algorithm1

#include <stdio.h>
#include <tchar.h>
#include <string>
#include <ctime>
#include <map>
#include <iostream>

using namespace std;

const int Rock = -2;
const int Paper = 0;
const int scissors = 1;


std::string str_symbols[3] = {"Rock", "Paper", "scissors"};
std::string results[3] = {"Win", "Lose", "Deuce"};
int symbols[3] = {Rock, Paper, scissors};

int CreateSymbol()
{
	srand((unsigned)time(0));
	return rand()%3;
}

int WhoWins(int x , int y)
{
	int a = symbols[x];
	int b = symbols[y];
	if (a==b)
	{
		return 2;//deuce
	}
	
	if (a*b==0)
	{
		return a > b ? 0:1;
	}

	return abs(a) > abs(b)? 0:1;
}

const int N = 4;
int _tmain(int argc, _TCHAR* argv[])
{
	int input;
	for (int i=0; i<N; i++)
	{
		while(1)
		{
			cout<<"please input a number 0, 1 or 2 ";
			cout<<"0-->Rock, 1-->Paper, 2-->scissors"<<endl;
			cin>>input;
			if (input>=0 && input <=2)
			{
				break;
			}
			cout<<"input is out of the range"<<endl;
		}

		int a = CreateSymbol();
		int result  = WhoWins(a, input);
		std::cout<<"computer gives a "<<str_symbols[a]<<std::endl;
		std::cout<<"you give a "<<str_symbols[input]<<std::endl;
		std::cout<<"computer "<<results[result]<<std::endl;
	}

	return 0;
}


Algorithm2

#include <stdio.h>
#include <tchar.h>
#include <string>
#include <ctime>
#include <map>
#include <iostream>
#include <algorithm>

using namespace std;

std::string str_symbols[3] = {"Rock", "Paper", "scissors"};
std::string results[3] = {"Win", "Lose", "Deuce"};
int rules[5] = {2, 0, 1, 2, 0};

int CreateSymbol()
{
	srand((unsigned)time(0));
	return rand()%3;
}

int WhoWins2(int x , int y)
{
	if (x == y)
	{
		return 2;
	}
	int *p_x= find(rules+1, rules+4, x);// find the element x from [rule[1]~rule[3]]
	int *p_y = find(p_x-1, p_x+2, y);// find the element y from near by element x;
	return p_x>p_y?0:1;
}

const int N = 4;
int _tmain(int argc, _TCHAR* argv[])
{
	int input;
	for (int i=0; i<N; i++)
	{
		while(1)
		{
			cout<<"please input a number 0, 1 or 2 ";
			cout<<"0-->Rock, 1-->Paper, 2-->scissors"<<endl;
			cin>>input;
			if (input>=0 && input <=2)
			{
				break;
			}
			cout<<"input is out of the range"<<endl;
		}

		int a = CreateSymbol();
		int result  = WhoWins2(a, input);
		std::cout<<"computer gives a "<<str_symbols[a]<<std::endl;
		std::cout<<"you give a "<<str_symbols[input]<<std::endl;
		std::cout<<"computer "<<results[result]<<std::endl;
	}
	return 0;
}

Summary

Algorithm2 is quicker if there are more variables. For example we have Tiger(0), Stick(1), Insect(2), Chicken(3), then we could establish a rule

int rule[] = {3, 0, 1, 2, 3, 0} 

int WhoWins(int x , int y)
{
	if (x == y)
	{
		return 2;
	}

	int *p_x= find(rules+1, rules+sizeof(rules)/sizeof(int)-2, x);
	if (y == *(p_x+1))
	{
		return 1;//lose
	}
	else if (y == *(p_x-1))
	{
		return 0;//win
	}
	else
	{
		return 2;//deuce
	}
}

j

你可能感兴趣的:(Game of Paper, Rock and scissors)