/* Brooks Guthrie - Assignment 06 The purpose of this program is to create an interactive interface for the game of Nim. Each iteration of the game begins with three rows of random length (from 1-10), with the text indicating which player's turn it is. */ int szx = 15; // Width of a single mark int szy = 35; // Height of a single mark int spc = 25; // Space between marks int spcy = 5; // Space between rows int turn = 1; // Will indicate which player's turn it is // Randomly generate the number of marks in a row and store them in a rows array for less code when creating the rows int row1 = int( random( 1,11 ) ); int row2 = int( random( 1,11 ) ); int row3 = int( random( 1,11 ) ); int rows[] = { row1,row2,row3 }; void setup() { size( 500,500 ); background( 0 ); noStroke(); // Sets up the board for the first time and indicates that it is player 1's turn for(int i = 0; i < rows.length; i++ ) { for( int j = 0; j < rows[i]; j++ ) { makeMark( i,j ); } } PFont font = loadFont( "ACaslonPro-Bold-48.vlw" ); textFont( font ); text( "Turn: Player 1", 0,height/2 ); } void draw() { //background( 0 ); fill( 255 ); /* For each iteration of draw(), the for loop is run and the board is drawn. If the mouse is pressed at a given point determined by check(), the row is cleared and the turn counter is increased */ if( mousePressed ) { background( 0 ); for(int i = 0; i < rows.length; i++ ) { for( int j = 0; j < rows[i]; j++ ) { if( check( i,j ) ) { clearRow( i,j ); turn++; break; } makeMark( i, j ); } } } mousePressed = false; pturn( turn ); // Changes the on-screen text to indicate which player should go next } // Draws the game board void makeMark( int i, int j ) { rect( j * spc, (spcy + szy) * i, szx, szy ); } // Determines if the mouse is clicked within a single mark. If so, returns true. boolean check( int i, int j ) { if( mouseX >= j * spc && mouseX<= ( j * spc ) + szx && mouseY >= (spcy + szy) * i && mouseY <= ((spcy + szy) * i) + szy ) { return( true ); } else return( false ); } /* If check() returns true, clearRow sets the new value of a given row to the current value of j, which in turn clears the clicked mark, and all marks to the right. */ void clearRow( int i,int j ) { rows[i] = j; } // Determines which player's turn it is, and upadates the text accordingly. void pturn( int turn ) { PFont font = loadFont( "ACaslonPro-Bold-48.vlw" ); textFont( font ); if( turn % 2 == 0 ) { fill( 150,0,0 ); text( "Turn: Player 2", 0,height/2 ); } else { fill( 0,150,0 ); text( "Turn: Player 1", 0,height/2 ); } }