50 likes | 223 Views
Four Way Connect Four Fall. Alicia Korpi. The purpose of the game is to get four of your color circles in a row. Unlike traditional ‘Connect Four’ games, the pieces can fall from each side of the board.
E N D
Four Way Connect Four Fall Alicia Korpi
The purpose of the game is to get four of your color circles in a row. • Unlike traditional ‘Connect Four’ games, the pieces can fall from each side of the board. • The circles will ‘fall’ to the other side of the board, unless they hit into another circle or they meet the ‘rocks.’ • The ‘rocks’ are four randomly placed roadblocks in each game so that the pieces can’t move past them. • The game is over when a player gets four of their circles in a row, and they win or there are no more spaces on the board and it is a tie.
for(int r= 0; r<10; r++) { int c = 0; while(c<10) { if (board[r][c]==1) { count1++; count2=0; } if(board[r][c]==2) { count2++; count1=0; } if(board[r][c]==0 || board[r][c]==3) { count1=0; count2=0; } if (count1==4) { isaWinner(1); returntrue; } if (count2==4) { isaWinner(2); returntrue; } c++; r++; } } • One of the more difficult aspects of this game was finding a simple way to check if there was a winner. • Unlike tic-tac-toe, it would be very complicated to check separately for any diagonal wins. • I used several for-loops in effort to reduce the amount code needed to determine if there was a winner.
if (r==0) { while(true) { int row = 0 + i; if (row==10) return; if(board[c-1][row]==0 && row==0) { board[c-1][row] = playerId; if (playerId==1) StdDraw.setPenColor(StdDraw.BLUE); if (playerId==2) StdDraw.setPenColor(StdDraw.RED); StdDraw.filledCircle(c+.5, row+1.5, .40); } elseif(board[c-1][row]==0 && row!=0) { //clear previous StdDraw.setPenColor(StdDraw.BLACK); StdDraw.filledSquare(c+.5, row+.5, .47); board[c-1][row-1]=0; board[c-1][row] = playerId; if (playerId==1) StdDraw.setPenColor(StdDraw.BLUE); if (playerId==2) StdDraw.setPenColor(StdDraw.RED); StdDraw.filledCircle(c+.5, row+1.5, .40); } elseif(board[c-1][row]!=0) { return; } StdDraw.show(500); i++; } • To make the circles ‘fall’ to the other side, I made a while loop for the animation. • When they were falling, before they could move to the next spot, I had to check to make sure that there were no other circles or ‘rocks’ in the next open spot.