Game of Paper, Rock and scissors

本文介绍了一个实现纸剪布游戏的人工智能算法,包括两种不同的实现方式:一种使用随机生成符号,另一种通过规则数组快速判断胜负。程序允许用户输入选择(0:石头,1:纸,2:剪刀),并显示计算机的选择与结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值