-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redesigned game class so that requestMove is nice and modular, operat…
…ing completely separate from the view. This will make communication with a server easy.
- Loading branch information
Showing
9 changed files
with
388 additions
and
363 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,96 @@ | ||
package controller; | ||
|
||
import java.io.Console; | ||
import java.util.ArrayList; | ||
|
||
import view.GamePanel; | ||
import model.Board; | ||
import model.Color; | ||
import model.Location; | ||
import model.Move; | ||
import model.Piece; | ||
import view.GamePanel; | ||
|
||
public class Game { | ||
private Board board; | ||
private Color current_turn; | ||
private GamePanel panel; | ||
private GamePanel gamePanel; | ||
private boolean inJumpSequence; | ||
|
||
|
||
public Game(Board board) { | ||
this.board = board; | ||
current_turn = Color.BLACK; | ||
this.inJumpSequence = false; | ||
} | ||
|
||
public void movePiece(Move move) { | ||
if (move.isJump()) { | ||
board.jump(move); | ||
panel.removePiece(new Location((move.destination.row | ||
+ move.source.row)/2, | ||
(move.source.column | ||
+ move.destination.column) / 2)); | ||
} else { | ||
board.move(move); | ||
/* Either the server or the user will request a move */ | ||
public void requestMove(Move move) { | ||
board.movePiece(move); | ||
gamePanel.movePiece(move); | ||
|
||
/* If this is the first jump of the jump sequence */ | ||
if (move.isJump() && !inJumpSequence) inJumpSequence = true; | ||
|
||
/* If the piece that jumped has no more jumps */ | ||
if (move.isJump() && getAvailableMoves(move.destination).isEmpty()) | ||
inJumpSequence = false; | ||
|
||
/* If the game is not in the middle of a jump sequence, move the thunk */ | ||
if (!inJumpSequence) { | ||
|
||
Move thunkMove = getThunkMove(); | ||
board.movePiece(thunkMove); | ||
gamePanel.movePiece(thunkMove); | ||
|
||
if (thunkMove.isJump()) inJumpSequence = true; | ||
|
||
while (inJumpSequence) { | ||
thunkMove = getThunkMove(); | ||
if (thunkMove != null) { | ||
board.movePiece(thunkMove); | ||
gamePanel.movePiece(thunkMove); | ||
} else { | ||
inJumpSequence = false; | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
public void switchTurn() { | ||
this.current_turn = this.current_turn == Color.BLACK ? | ||
Color.WHITE : Color.BLACK; | ||
public Move getThunkMove() { | ||
ArrayList<Move> availableMoves; | ||
|
||
if (inJumpSequence) { | ||
availableMoves = | ||
board.generateJumpMovesForPiece(board.getLastPieceMoved()); | ||
} else { | ||
availableMoves = board.generateAllMoves(GameConstants.THUNK_COLOR); | ||
} | ||
|
||
if (!availableMoves.isEmpty()) { | ||
/* Just take the first move we see */ | ||
Move moveChoice = availableMoves.get(0); | ||
return moveChoice; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
|
||
public ArrayList<Move> getAvailableMoves(Location source) { | ||
Piece moved = board.getLastPieceMoved(); | ||
if (moved != null && moved.color == current_turn) { | ||
// player just jumped | ||
ArrayList<Move> jumpset = board.generateJumpMoves(moved); | ||
if (jumpset.isEmpty()) { | ||
switchTurn(); | ||
} | ||
return jumpset; | ||
if (inJumpSequence) { | ||
return board.generateJumpMovesForPiece(board.getPiece(source)); | ||
} else { | ||
return board.generateMovesForPiece(board.getPiece(source)); | ||
} | ||
ArrayList<Move> moves = board.generateMoves(board.getPiece(source)); | ||
ArrayList<Move> jumps = board.generateJumpMoves(board.getPiece(source)); | ||
ArrayList<Move> allMoves = new ArrayList<Move>(moves); | ||
allMoves.addAll(jumps); | ||
return allMoves; | ||
} | ||
|
||
public void playVsThunk() { | ||
Color USER_COLOR = Color.BLACK; | ||
Color THUNK_COLOR = Color.WHITE; | ||
while (true) { | ||
try { | ||
Thread.sleep(200); | ||
} catch (InterruptedException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
if (this.board.getMovesSinceCapture() > 50) break; | ||
if (this.current_turn == THUNK_COLOR) { | ||
Piece moved = this.board.getLastPieceMoved(); | ||
if (moved.color == THUNK_COLOR) { | ||
// thunk just jumped | ||
ArrayList<Move> jumpset = board.generateJumpMoves(moved); | ||
if (jumpset.isEmpty()) { | ||
switchTurn(); | ||
continue; | ||
} | ||
Move jump = jumpset.get(0); | ||
movePiece(jump); | ||
panel.moveArbitraryPiece(jump); | ||
} | ||
else { | ||
ArrayList<Move> moveset = board.generateAllPossibleMoves(THUNK_COLOR); | ||
if (moveset.isEmpty()) { | ||
System.out.println("Thunk is out of moves."); | ||
break; | ||
} | ||
Move theMove = moveset.get(0); | ||
movePiece(theMove); | ||
panel.moveArbitraryPiece(theMove); | ||
|
||
if(!theMove.isJump()) { | ||
switchTurn(); | ||
} | ||
} | ||
} | ||
if (this.current_turn == USER_COLOR) { | ||
//ArrayList<Move> moveset = board.generateAllPossibleMoves(USER_COLOR); | ||
//if (moveset.isEmpty()) break; | ||
} | ||
|
||
//board.print(); | ||
} | ||
Color winner = current_turn == Color.BLACK ? Color.WHITE : Color.BLACK; | ||
System.out.println("THE WINNER IS " + winner.toString()); | ||
public ArrayList<Move> getAllAvailableJumpMoves(Color player) { | ||
return board.generateAllJumpMoves(player); | ||
} | ||
|
||
|
||
public void setGamePanel(GamePanel panel) { | ||
this.panel = panel; | ||
this.gamePanel = panel; | ||
} | ||
|
||
public Color getCurrentTurn() { | ||
return current_turn; | ||
public boolean isInJumpSequence() { | ||
return inJumpSequence; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package controller; | ||
|
||
import model.Color; | ||
|
||
public class GameConstants { | ||
public static final int USER_MODE = 0; | ||
public static final int SERVER_MODE = 0; | ||
public static final Color THUNK_COLOR = Color.WHITE; | ||
public static final Color USER_COLOR = Color.BLACK; | ||
} |
Oops, something went wrong.