Full description here if you guys need to reference it. I am just looking for some guidance. I am lost and would greatly appreciate any help.
CS 302, Program 4
I need to get a basic game going by expanding your work from the first two steps. Now only allow users to place disks in squares that correspond to valid moves and flip any disks that this move captured. Note that a player might capture disks on a horizontal, vertical, or diagonal line (or any combination of the three) between the newly placed piece and any of the player's old pieces. Also make sure to check if the game is over after each move. If the game is over, calculate who wins and pass all this information to the gui through the update method.
Here is my code thus far. I am stuck on where to start the algorithm :
Board class:
import java.util.*; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; public class Board { public final int rows = 8; public final int columns = 8; public String title = "Reversi"; public Board(){ } public void processButtons(int[][] board, boolean gameOver, boolean moveMade, int gameWinner, ReversiGUI gui){ int turn = 1; do{ moveMade = true; ReversiAction action = gui.getMouseInput(); if(action.ACTION_TYPE == ReversiAction.LEFT_CLICK){ if(turn > 0){ board[action.ROW][action.COLUMN] = ReversiGUI.BLACK; } else{ board[action.ROW][action.COLUMN] = ReversiGUI.WHITE; } gui.update(board, gameOver, gameWinner, true); turn *= -1; } if(action.ACTION_TYPE == ReversiAction.NEW_GAME){ moveMade = false; gameWinner = 0; gameOver = false; Application.display(board, title, gameOver, gameWinner, moveMade, gui); } if(action.ACTION_TYPE == ReversiAction.QUIT){ gui.quit(); } if(action.ACTION_TYPE == ReversiAction.SAVE_GAME){ save(gui, board); } if(action.ACTION_TYPE == ReversiAction.LOAD_GAME){ load(gui, board); } }while(!gameOver); } public void save(ReversiGUI gui, int[][] board){ File file = gui.getSaveFile(); String[]stringBoard = new String[8]; try{ PrintWriter save = new PrintWriter(file); stringBoard[0]= "8 8 " + gui.getCurrentPlayer().getColor(); save.println(stringBoard[0]); for(int i = 0; i < rows; i++){ stringBoard[i] = ""; for(int j = 0; j < columns; j++){ if(board[i][j] == ReversiGUI.BLACK){ stringBoard[i] += "B"; } if(board[i][j] == ReversiGUI.WHITE){ stringBoard[i] += "W"; } if(board[i][j] == ReversiGUI.EMPTY){ stringBoard[i] += "0"; } } save.println(stringBoard[i]); } save.close(); } catch(FileNotFoundException e){ System.out.println("file not found"); } } public void load(ReversiGUI gui, int[][] board){ File file = gui.getLoadFile(); } }
Application class:
import java.util.*; public class Application { public static void main (String[] args){ Board gameBoard = new Board(); boolean gameOver = false; int gameWinner = 0; boolean moveMade = false; int[][] board = new int[gameBoard.rows][gameBoard.columns]; ReversiGUI gui = new ReversiGUI(gameBoard.title, gameBoard.rows, gameBoard.columns); display(board, gameBoard.title, gameOver, gameWinner, moveMade, gui); gameBoard.processButtons(board, gameOver, moveMade, gameWinner, gui); //gui.update(board, gameOver, gameWinner, moveMade); } public static void display(int[][] board, String title, boolean gameOver, int gameWinner, boolean moveMade, ReversiGUI gui){ final int rows = 8; final int columns = 8; for(int i = 0; i < rows; i++){ for(int j = 0; j < columns; j++){ if((i == 4 && j == 4) || (i == 3 && j == 3)){ board[i][j] = ReversiGUI.BLACK; } else if((i == 3 && j == 4) || (i == 4 && j == 3)){ board[i][j] = ReversiGUI.WHITE; } else{ board[i][j] = ReversiGUI.EMPTY; } } } gui.update(board, gameOver, gameWinner, moveMade); } }