Hello all!
Our assignment is to create a playable Reversi/Othello game. I can’t get the game pieces to translate. They are stuck at 0,0.
#include <iostream> //include iostream file content
#include <cmath> //include cmath, use in calculations for drawing circles
using namespace std; //use the standard namespace
#ifdef __APPLE__
# include <OpenGL/gl.h> //definitions for GL graphics routines
# include <OpenGL/glu.h> //definitions for GL input device handling
# include <GLUT/glut.h> //definitions for GLUT utility toolkit
#else
# include <GL/gl.h> //definitions for GL graphics reoutines
# include <GL/glu.h> //definitions for GL input device handling
# include <GL/glut.h> //definitions for GLUT utility toolkit
#endif
#define PI 3.1415926536 //used in making circles
//board size
#define BOARDSIZE 800 //stands in for both height and width of the program
//game piece colors
#define EMPTY 0 //define numeric setting for empty game board squares
#define BLACK -1 //define numeric setting for black game board squares
#define WHITE 1 //define numeric setting for white game board squares
//row and colums
#define ROWCOUNT 8 //total number of grid rows
#define COLUMNCOUNT 8 //total number of grid columns
//grid colors
#define GlineColor 0, 0, 0 //R,G,B color settings of grind lines
#define GboxColor .7, .9, .5, 0 //R,G,B,A color settings of grind squares
int board[BOARDSIZE][BOARDSIZE]; /* make board global since all routines use it */
void circle(int i) //circle definintion
{
int k; //loop counter
float angle, ainc; //angle & angle incriment
ainc = 2 * PI / i;
glBegin(GL_POLYGON);
for(k=0; k<i; k++)
{
angle = k*ainc;
glVertex2f(cos (angle)*.5, sin(angle)*.5); //h,w of circle
}
glEnd();
}//end void circle
/*
Clear the board and place the initial 4 checkers in the center
*/
void initboard(){
int i, j;
for(i = 0; i < BOARDSIZE; i++)
for(j = 0; j < BOARDSIZE; j++)
board[i][j] = EMPTY;
board[BOARDSIZE / 2 - 1][BOARDSIZE / 2 - 1] = WHITE;
board[BOARDSIZE / 2][BOARDSIZE / 2] = WHITE;
board[BOARDSIZE / 2 - 1][BOARDSIZE / 2] = BLACK;
board[BOARDSIZE / 2][BOARDSIZE / 2 - 1] = BLACK;
}
void drawGrid() //create grid lines for the 8x8 gameboard square matrix
{
int row; //game board rows
int col; //game board columns
glColor3f(GlineColor); //gameboard grid line color = GlineColor's R,G,B definition
glLineWidth(2); //grid line width, in pixels
glBegin(GL_LINES); //draws the horizontal grid lines
for(row = 0; row <= ROWCOUNT; row++){
glVertex2i(0, row);
glVertex2i(COLUMNCOUNT, row);}
glEnd();
glBegin(GL_LINES); //draws the vertical grid lines
for(col = 0; col <= COLUMNCOUNT; col++){
glVertex2i(col, 0);
glVertex2i(col, ROWCOUNT);}
glEnd();
}// close drawGrid
//GAME PIECES CREATION
void drawPieces()
{
int i,j;
/* set counters to look at all i,j gameboard matrix locations, and for any locations == WHITE
or == BLACK, draw in appropriate color game piece at those i,j locations */
for(i=0; i<BOARDSIZE; i++)
{
for(j=0; j<BOARDSIZE; j++)
{
if (board[i][j]==WHITE) /*Draws a white game piece for any i,j location that == WHITE*/
{
glBegin(GL_POLYGON);{
glPushMatrix();
glColor3f(1,1,1);
glTranslatef(i+.5,j+.5, 0);
glScalef(1,1,1);
circle(32);
glPopMatrix();}
glEnd();
}
}//close inner for (j counter)
}//end double for loop
}//end drawPieces
void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT);
drawGrid();
drawPieces();
glutSwapBuffers();
}//close drawScene
/*Display Callback Routine: clears the screen and draws our grid, when the program
is first run and anytime the the window is redrawn.*/
//Callback to handle keyboard pressing of q: quit when q key pressed
void handleKey(unsigned char key, int x, int y)
{
switch(key)
{
case 'q':
case 'Q':
exit(0);
}
}
//Mouse button event handler, for button pressed or released
void handleButtons(int button, int state, int x, int y)
{
int row, col;
float dx, dy;
if(button != GLUT_LEFT_BUTTON || state != GLUT_UP)
return;
y = BOARDSIZE - y;
dy = (float)BOARDSIZE / ROWCOUNT;
dx = (float)BOARDSIZE / COLUMNCOUNT;
row = (int)(y / dy);
col = (int)(x / dx);
cout << "button click in cell (" << row << ", " << col << ")
";
}
//event handler, for when window is resized by user
void handleResize(int w, int h)
{
w, h = BOARDSIZE; //sets width and heght equal to BOARDSIZE
glViewport(0, 0, BOARDSIZE, BOARDSIZE);//view port dimensions
//lower left is 0, 0
//upper right is BOARDSIZE, BOARDSIZE
glMatrixMode(GL_PROJECTION); //drawing coordinates, based on rows/cols
glLoadIdentity();
gluOrtho2D(0, COLUMNCOUNT, 0, ROWCOUNT);
glMatrixMode(GL_MODELVIEW);
}
//MAIN----------------------------------------------------------------------------
//draws the grid, and accept mouse clicks
int main(int argc, char* argv[ ]){
glutInit(&argc, argv); //arguement count and value
initboard();//PROGRAM WINDOW INITIALIZATION
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);//initialize window and set color mode
glutInitWindowSize(BOARDSIZE, BOARDSIZE);//dimensions of program window
glutCreateWindow("Reversi by Phelps Gaming, the Makers of.....uh.....?");//window title
glClearColor(GboxColor); //sets background color to GboxColor's R,G,B,A definition
//CALLBACK ROUTINES
glutDisplayFunc(drawScene);//drawing callback routine
glutMouseFunc(handleButtons);//mouse button callback routine
glutKeyboardFunc(handleKey);//keyboard key press routine, uses by glutMainLoop()
glutReshapeFunc(handleResize);//window resize callback routine
int i, j;
int numcaptured[3][3];
int totalcaptured;
glutMainLoop();
return 0;
}