下面的代码是我的石头剪刀布的实现。但是,问题是用户和计算机的动作不同步。
这是我执行它时发生的事情:
首先是计算机的选择(它显示“纸”)。然后当我选择我的移动(岩石)时,它会显示它的移动(岩石)。这是第二次(纸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();
}