以下代码是我对摇滚,纸张,剪刀的实施。但是,问题是用户和计算机的移动不同步。
这是我执行时发生的事情:
首先可以选择计算机(它显示'纸')。 然后,当我选择我的移动(摇滚)时,它显示了它的移动(摇滚)。 这是第二次(纸v / s摇滚)。 然后在第三次当我给我的动作(剪刀)并且它移动(纸张)时,结果显示为“绑定”,但我应该赢了。
我无法理解代码是如何做到的,因为我在没有OpenGL部分的情况下使用c ++实现了逻辑,并且工作正常。
有人可以在代码中看到问题吗?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<GL/glut.h>
int a; //GLOBAL VARIABLE TO CHECK USER'S CHOICE IN menu()
int n=0,sa,sb; //n IS NO. OF MOVES WE WANT- WE HAVE 3 MOVES IN THIS PROGRAM. sa & sb ARE THE SCORES OF USER AND COMPUTER RESPECTIVELY
char ch;
int comp_move();//THIS CALCULATES COMPUTER'S MOVE
void check(int,int);//CHECKS WHO HAS WON/LOST IN ONE PARTICULAR MOVE
int comp_move()
{
int c=0;
c=rand()%3;
if(c==1)drawrock(newrock);
if(c==2)drawpaper(newpaper);
if(c==3)drawscissor(newscissor);
return c;
}
void check(int b)
{
if(a==1) //rock is player's selection
{
if(b==2)sb++;
if(b==3)sa++;
}
else if(a==2) //paper is player's selection
{
if(b==1) sa++;
if(b==3) sb++;
}
else //scissor is player's selection
{
if(b==1)sb++;
if(b==2)sa++;
}
}
void menu(int id) //THE MENU FUNCTION
{
switch(id)
{
case 1: a=1;n++;
break;
case 2: a=2;n++;
break;
case 3: a=3;n++;
break;
}
glutPostRedisplay();
}
void display()
{
//glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
if(a==1)drawrock(rock);
if(a==2)drawpaper(paper);
if (a==3)drawscissor(scissor);
//for(int i=1;i<=n;i++)
int b=comp_move();
check(b);
if(n==3)//3 rounds
{
if(sa>sb)
sprint(150,450,a);//prints win
else if(sa==sb)
sprint(150,450,a);//prints loss
else
sprint(150,450,a);//prints tied
}
if(n>4) exit(0); //here i want to see the computer's third move
glFlush();
}
void MYINIT()
{
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
glMatrixMode(GL_MODELVIEW);
}
int main()
{
glutInitWindowSize(500,500);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutCreateWindow("rock, paper, scissors");
glutCreateMenu(menu);
glutAddMenuEntry("Rock",1);
glutAddMenuEntry("Paper",2);
glutAddMenuEntry("Scissors",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
MYINIT();
glutDisplayFunc(display);
glutMainLoop();
}
答案 0 :(得分:4)
该行
c=rand()%3;
将产生0
和2
之间的值,而不是1
和3
。替换为:
c = 1 + (rand()%3);