以下代碼是我對岩石,紙張,剪刀的執行。但是,問題在於用戶和計算機的移動不同步。爲什麼我的石頭,紙張,剪刀不能給出正確的結果?
這裏的時候我執行時發生了什麼:
首先進行的是計算機的選項(它顯示「紙」)。 然後,當我選擇我的舉動(搖滾)時,它顯示了它的舉動(搖滾)。 這是第二次(紙V/S搖滾)。 然後在第三次我給我的舉動(剪刀),它給了它的舉動(紙)結果顯示爲「並列」,但我應該贏了。
我無法理解代碼如何執行此操作,因爲我已經使用C++實現了沒有OpenGL部分的邏輯,並且它工作正常。
有人可以在代碼中看到問題嗎?
#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();
}
不要只是放棄你的代碼,請問一個具體的問題出了什麼問題,只需添加相關的代碼。例如。 opengl代碼並不重要。 – rekire
感謝您的建議。我剛剛編輯它。希望現在有所幫助。請回復 – user2281204
'c = rand()%3;'會得到'0'和'2'之間的值,而不是'1'和'3'。 – didierc