File: game.c

package info (click to toggle)
glut 3.6-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 9,084 kB
  • ctags: 15,234
  • sloc: ansic: 131,032; makefile: 2,129; ada: 2,012; yacc: 473; fortran: 290; lex: 131; sed: 49; csh: 38
file content (172 lines) | stat: -rw-r--r-- 3,731 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*  
 *  CS 453 - Final project : An OpenGL version of the pegboard game IQ
 *  Due : June 5, 1997
 *  Author : Kiri Wagstaff
 *
 *  File : game.c
 *  Description : All the routines to actually play the game.
 *  
 */

#include "gliq.h"

int playdone=0;

void playgame(void);
void drawquit(float x, float y, float r1, float r2);
int legalmove(void);
int canmove(int peg);
int movesexist(void);

void playgame(void)
{
  int height = glutGet(GLUT_WINDOW_HEIGHT);
  int width = glutGet(GLUT_WINDOW_WIDTH);

  /* Draw the quit button */
  drawquit(7.0, 9.0, 0.4, 1.0);
  /* Quit */
#if 0
  //  glColor3f(1.0, 1.0, 1.0);  /* white */
  //  text(0.78*width, 0.88*height, 0.1*height, "Quit");
#endif
  
  /* Draw the current scores */
  /* Draw the total # of pegs */
  glPushMatrix();
    glColor3f(1.0, 1.0, 0.0);     /* yellow */
    glTranslatef(-7.8, 8.8, 0.0);
    drawpeg();
    text(0.1*width, 0.9*height, 0.07*height, ": %02d", pegs);
  glPopMatrix();

  if (playdone)
    text(0.2*glutGet(GLUT_WINDOW_WIDTH),
	 0.75*glutGet(GLUT_WINDOW_HEIGHT),
	 0.08*glutGet(GLUT_WINDOW_HEIGHT),
	 "No moves left.");

  /* do the trackball rotation. */
  glPushMatrix();
  glRotatef(45.0, 1.0, 0.0, 0.0);
  tbMatrix();

  drawboard();
  drawpegs();
  glPopMatrix();
}

int canmove(int peg)
{
  int i, j;

  if (peg == 0)
    return 0;
  
  i = (peg-1)/BOARDSIZE;
  j = (peg-1)%BOARDSIZE;
  
  if ((i-2>0) && (filled[i-1][j]==FULL) && (filled[i-2][j]==EMPTY))
    return 1;
  else if ((i+2<BOARDSIZE) &&
	   (filled[i+1][j]==FULL) && (filled[i+2][j]==EMPTY))
    return 1;
  else if ((j-2>0) && (filled[i][j-1]==FULL) && (filled[i][j-2]==EMPTY))
    return 1;
  else if ((j+2<BOARDSIZE) &&
	   (filled[i][j+1]==FULL) && (filled[i][j+2]==EMPTY))
    return 1;
  else
    return 0;
  
}

/** returns 0 if not a legal move;
 ** returns the index of the middle (jumped) peg if it was legal
 ** (1 to BOARDSIZE*BOARDSIZE)
 **/

int legalmove(void)
{
  int lasti, lastj;
  int i, j;

  if (lastpicked == 0)
    return 0;
  
  lasti = (lastpicked-1)/BOARDSIZE;
  lastj = (lastpicked-1)%BOARDSIZE;
  i = (picked-1)/BOARDSIZE;
  j = (picked-1)%BOARDSIZE;
#if 0
  //  printf("Jumping from (%d,%d) to (%d,%d)\n", lasti, lastj, i, j);
#endif
  
  if (filled[lasti][lastj] == CANMOVE && filled[i][j] == EMPTY)
    if (lasti==i+2)
      return (i+1)*BOARDSIZE+(j)+1;  /* i+1, +1 to get the name right */
    else if (lasti==i-2)
      return (i-1)*BOARDSIZE+(j)+1;  /* i-1, +1 */
    else if (lastj==j+2)
      return (i)*BOARDSIZE+(j+1)+1;  /* j+1, +1 */
    else if (lastj==j-2)
      return (i)*BOARDSIZE+(j-1)+1;  /* j-1, +1 */
    else
      return 0;
  return 0;
}

/* Checks for any legal moves remaining */
int movesexist(void)
{
  int i, j, peg;
  
  for (peg=1; peg<=BOARDSIZE*BOARDSIZE; peg++)
    {
      i = (peg-1)/BOARDSIZE;
      j = (peg-1)%BOARDSIZE;
      if (filled[i][j] == FULL && canmove(peg))
	return 1;
    }
  return 0;
}

void drawquit(float x, float y, float r1, float r2)
{
  GLUquadricObj* stick;

  glLoadName(QUIT);
#if 0
  //glDisable(GL_LIGHTING);
#endif
  glColor3f(1.0, 0.0, 0.0);  /* red */
#if 0
  //glRectf(x, y, x+w, y+h);
  //glEnable(GL_LIGHTING);
#endif

  glPushMatrix();
    glTranslatef(x, y, 0.0);
    glPushMatrix();
      stick = gluNewQuadric();
      glRotatef(90, 0.0, 1.0, 0.0);
      glRotatef(45, 1.0, 0.0, 0.0);
      glTranslatef(0.0, 0.0, r2-1.5*r1);
      gluQuadricDrawStyle(stick, GLU_FILL);
      gluQuadricNormals(stick, GLU_SMOOTH);
      gluCylinder(stick, 0.85*r1, 0.85*r1, 3*r1, 8, 1); 
      gluDeleteQuadric(stick);
    /*    glutSolidCone(rad, len, 8, 8);*/
  glPopMatrix();

  glRotatef(22.5, 0.0, 0.0, 1.0);
  glutSolidTorus(r1, r2, 8, 8);
  glPopMatrix();
    
  glLoadName(0);

}