Last active
March 17, 2019 10:51
-
-
Save wightwulf1944/c300060a3a9dae2577f48ca554a7bfbe to your computer and use it in GitHub Desktop.
TicTacToy engine
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
package model; | |
public class Board { | |
private final Piece[][] cells; | |
public Board(int width, int height) { | |
cells = new Piece[width][height]; | |
} | |
public Piece getPiece(int x, int y) { | |
return cells[x][y]; | |
} | |
public void setPiece(Piece piece, int x, int y) { | |
cells[x][y] = piece; | |
} | |
public boolean isCellEmpty(int x, int y) { | |
return getPiece(x, y) == null; | |
} | |
} |
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
package model; | |
import constant.Type; | |
import constant.WinState; | |
public class Game { | |
private static final int BOARD_WIDTH = 3; | |
private static final int BOARD_HEIGHT = 3; | |
private final Board board; | |
private final Player player1; | |
private final Player player2; | |
private Player activePlayer; | |
public Game() { | |
board = new Board(BOARD_WIDTH, BOARD_HEIGHT); | |
player1 = new Player(Type.X); | |
player2 = new Player(Type.O); | |
activePlayer = player1; | |
} | |
public void forfeit() { | |
if (activePlayer == player1) { | |
player1.setWinState(WinState.LOSER); | |
player2.setWinState(WinState.WINNER); | |
} else { | |
player2.setWinState(WinState.LOSER); | |
player1.setWinState(WinState.WINNER); | |
} | |
} | |
public void setPlayer1Name(String name) { | |
player1.setName(name); | |
} | |
public void setPlayer2Name(String name) { | |
player2.setName(name); | |
} | |
public void play(int x, int y) { | |
if (board.isCellEmpty(x, y)) { | |
Piece piece = activePlayer.getPiece(); | |
board.setPiece(piece, x, y); | |
} | |
for (int iX = 0; iX < BOARD_WIDTH; iX++) { | |
for (int iY = 0; iY < BOARD_HEIGHT; iY++) { | |
// TODO iterate over the board and see if activePlayer has won | |
} | |
} | |
activePlayer = activePlayer == player1 ? player2 : player1; | |
} | |
} |
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
package model; | |
public class Piece { | |
private final char type; | |
public Piece(char type) { | |
this.type = type; | |
} | |
public char getType() { | |
return type; | |
} | |
} |
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
package model; | |
public class Player { | |
private final char type; | |
private String name; | |
private int winState; | |
public Player(char type) { | |
this.type = type; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public Piece getPiece() { | |
return new Piece(type); | |
} | |
public int getWinState() { | |
return winState; | |
} | |
public void setWinState(int winState) { | |
this.winState = winState; | |
} | |
} |
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
package constant; | |
public class Type { | |
public static final char X = 'x'; | |
public static final char O = 'O'; | |
} |
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
package constant; | |
public class WinState { | |
public static final int PLAYING = 0; | |
public static final int WINNER = 1; | |
public static final int LOSER = 2; | |
public static final int DRAW = 3; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment