Commit c41c81cc authored by mva021's avatar mva021
Browse files

Updated Comments on GUI classes and renamed ConnectFour

parent 0a460a98
......@@ -15,12 +15,27 @@ import inf101.v20.sem2.grid.Location;
import inf101.v20.sem2.games.IGame;
import inf101.v20.sem2.games.Player;
/**
* This class is a grid of Game panels.
* This is also a MouseListener for all those GamePanels
* so whenever any of the panels in the grid is clicked
* a method in this class is envoked.
*
* This click will be translated into a Location and a
* move will be sent to the game.
*
* @author Anna Eilertsen - anna.eilertsen@uib.no
* @author Martin Vatshelle - martin.vatshelle@uib.no
*
*/
public class ClickableGrid extends MouseAdapter {
private JPanel pane;
private Grid<GamePanel> clickablePanels; //clickable grid for user input
private IGame game;
//HashMap is something we will learn about in INF102
HashMap<Player,Color> color;
public ClickableGrid(IGame game,List<Color> colors) {
......@@ -43,6 +58,14 @@ public class ClickableGrid extends MouseAdapter {
pane.validate();
}
/**
* Initializes a Map from Player to Color.
* This is later used color the grid of GamePanel to match
* the moves of the different players.
*
* @param game
* @param colors
*/
private void setColors(IGame game, List<Color> colors) {
color = new HashMap<Player, Color>();
for(Player p : game.players()) {
......@@ -62,6 +85,13 @@ public class ClickableGrid extends MouseAdapter {
pane.updateUI();
}
/**
* Finds the color to use on those Locations where the given
* player has placed.
*
* @param player
* @return
*/
protected Color getColor(Player player) {
return color.get(player);
}
......
package inf101.v20.sem2.GUI;
import inf101.v20.sem2.games.AbstractGame;
import inf101.v20.sem2.games.TicTacToe;
public class GUIMain {
public static void main(String[] args) {
AbstractGame game = new TicTacToe(GameGUI.getPlayers());
MNKGameGUI gameGUI = new MNKGameGUI(game);
GameGUI gui = new GameGUI(gameGUI);
}
}
\ No newline at end of file
......@@ -10,12 +10,20 @@ import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import inf101.v20.sem2.games.FourInRow;
import inf101.v20.sem2.games.ConnectFour;
import inf101.v20.sem2.games.AbstractGame;
import inf101.v20.sem2.games.MiniMaxPlayer;
import inf101.v20.sem2.games.Player;
import inf101.v20.sem2.games.TicTacToe;
/**
* This class combines two buttons with a MNKGameGUI in one JFrame
* The buttons are used to select type of game to play.
*
* @author Anna Eilertsen - anna.eilertsen@uib.no
* @author Martin Vatshelle - martin.vatshelle@uib.no
*
*/
public class GameGUI implements ActionListener{
private JButton playConnectFourButton; //Button to start new 4 in row game
......@@ -34,7 +42,18 @@ public class GameGUI implements ActionListener{
drawGameBoard();
}
public static void main(String[] args) {
AbstractGame game = new TicTacToe(GameGUI.getPlayers());
MNKGameGUI gameGUI = new MNKGameGUI(game);
new GameGUI(gameGUI);
game.start();
}
/**
* Draws the gameBoard on the JFrame
* and refreshes the rest of the graphics
*/
private void drawGameBoard() {
//add the clickable grid panel
frame.add("Center", gameBoard);
......@@ -75,7 +94,7 @@ public class GameGUI implements ActionListener{
Iterable<Player> players = getPlayers();
frame.remove(gameBoard);
if(e.getSource() == playConnectFourButton) {
this.gameBoard = new MNKGameGUI(new FourInRow(players));
this.gameBoard = new MNKGameGUI(new ConnectFour(players));
}
if(e.getSource() == playTicTacToeButton) {
this.gameBoard = new MNKGameGUI(new TicTacToe(players));
......@@ -84,6 +103,11 @@ public class GameGUI implements ActionListener{
drawGameBoard();
}
/**
* Generates a list of players based on user input
*
* @return an Iterable of 2 Players
*/
public static Iterable<Player> getPlayers() {
ArrayList<Player> players = new ArrayList<Player>();
//add player1
......
......@@ -9,8 +9,11 @@ import javax.swing.JPanel;
/**
* A class for clickable panels with x,y-coordinates
* This is basically a Jpanel with a mouse listener and a color.
* When this panel is clicked a call to the MouseListener will be made
*
* @author anna
* @author Anna Eilertsen - anna.eilertsen@uib.no
* @author Martin Vatshelle - martin.vatshelle@uib.no
*
*/
public class GamePanel extends JPanel{
......@@ -37,6 +40,13 @@ public class GamePanel extends JPanel{
return color;
}
/**
* Sets the color of this panel.
* When the updateUI function is called the color
* will be changed to this color
*
* @param color
*/
public void setColor(Color color) {
this.color = color;
}
......
......@@ -12,8 +12,10 @@ import javax.swing.JPanel;
import inf101.v20.sem2.games.AbstractGame;
/**
* This combines a message field and a clickable grid in one JPanel
*
* @author Anna Eilertsen
* @author Anna Eilertsen - anna.eilertsen@uib.no
* @author Martin Vatshelle - martin.vatshelle@uib.no
*/
public class MNKGameGUI extends JPanel{
......@@ -44,15 +46,11 @@ public class MNKGameGUI extends JPanel{
validate();
}
public void reset(AbstractGame newGame) {
this.game = newGame;
this.game.setGui(this);
//updateMessage();
removeAll();
initialize();
this.updateUI();
}
/**
* Displays a message above the clickable grid of GamePanels
*
* @param message
*/
public void updateMessage(String message) {
statusMessage.setText(message);
}
......@@ -73,6 +71,10 @@ public class MNKGameGUI extends JPanel{
return new Dimension(100*game.getGameBoard().numRows(), 100*game.getGameBoard().numRows());
}
/**
* Sets the Color of all the GamePanels in the clickable grid to match the game
* Then calls the method updateUI in JPanel.
*/
@Override
public void updateUI() {
if(clickablePanels!=null)
......
......@@ -99,6 +99,7 @@ public abstract class AbstractGame implements IGame {
if(gui != null) {
gui.updateUI();
}
showMessage(getCurrentPlayer() +" it is your turn.");
}
private boolean currentIsGuiPlayer(){
......@@ -183,16 +184,21 @@ public abstract class AbstractGame implements IGame {
private void printResult() {
for(Player p : players) {
if(isWinner(p)) {
System.out.println(p+ " is the winner!");
showMessage(p+ " is the winner!");
return;
}
}
if(gameOver())
System.out.println("It is a draw!");
else
System.out.println("The game is ongoing!");
if(gameOver()) {
showMessage("It is a draw!");
}else
showMessage("The game is ongoing!");
}
private void showMessage(String message) {
System.out.println(message);
if(gui!=null)
gui.updateMessage(message);
}
/**
* Checks if there is a winner in the game
* @return
......@@ -223,8 +229,13 @@ public abstract class AbstractGame implements IGame {
return hasWinner() || isDraw();
}
/**
* Checks if the given player has any possible moves
* @param p
* @return
*/
public boolean hasPossibleMoves(Player p) {
return !board.isFull();
return !possibleMoves(p).isEmpty();
}
@Override
......
......@@ -11,17 +11,17 @@ import inf101.v20.sem2.grid.Location;
* @author Martin Vatshelle - martin.vatshelle@uib.no
*
*/
public class FourInRow extends AbstractGame {
public class ConnectFour extends AbstractGame {
public FourInRow() {
public ConnectFour() {
super(new GameBoard(6, 7));
}
public FourInRow(Iterable<Player> players) {
public ConnectFour(Iterable<Player> players) {
super(new GameBoard(6, 7),players);
}
public FourInRow(GameBoard board, Iterable<Player> players) {
public ConnectFour(GameBoard board, Iterable<Player> players) {
super(board,players);
}
......@@ -51,6 +51,11 @@ public class FourInRow extends AbstractGame {
}
/**
* This method disregards the row number of the input location and finds
* The lowest row number such that it is possible to place on that position.
* If no location in that column is possible to place on the highest
* row number on the board is used.
*
* @param loc
* @return
*/
......@@ -67,6 +72,13 @@ public class FourInRow extends AbstractGame {
return dropFrom(new Location(0, column));
}
/**
* This method starts from a location and moves down
* until it can not move further.
*
* @param loc
* @return
*/
Location dropFrom(Location loc) {
Location below = loc.move(GridDirection.SOUTH);
while(board.validLocation(below) && board.isEmpty(below)){
......@@ -75,9 +87,10 @@ public class FourInRow extends AbstractGame {
}
return loc;
}
@Override
public AbstractGame copy() {
AbstractGame newGame = new FourInRow(board.copy(),players);
AbstractGame newGame = new ConnectFour(board.copy(),players);
newGame.setPlayer(getCurrentPlayer());
return newGame;
}
......
......@@ -6,7 +6,7 @@ import java.util.Scanner;
import inf101.v20.sem2.grid.Location;
import inf101.v20.sem2.games.AbstractPlayer;
import inf101.v20.sem2.games.FourInRow;
import inf101.v20.sem2.games.ConnectFour;
import inf101.v20.sem2.games.IGame;
/**
......@@ -57,8 +57,8 @@ public class ConsolePlayer extends AbstractPlayer {
@Override
public Location getMove(IGame game) {
game.getGameBoard().printBoard();
if(game instanceof FourInRow) {
return getColumn((FourInRow) game);
if(game instanceof ConnectFour) {
return getColumn((ConnectFour) game);
}
else
return getLocation(game);
......@@ -67,7 +67,7 @@ public class ConsolePlayer extends AbstractPlayer {
/**
* Reads input for the FourInARow game
*/
private Location getColumn(FourInRow game) {
private Location getColumn(ConnectFour game) {
System.out.println("Player "+this.toString()+" type [Col] to make a move.");
boolean done = false;
int col = 0;
......
......@@ -3,7 +3,7 @@ package inf101.v20.sem2.terminal;
import java.util.ArrayList;
import java.util.Scanner;
import inf101.v20.sem2.games.FourInRow;
import inf101.v20.sem2.games.ConnectFour;
import inf101.v20.sem2.games.IGame;
import inf101.v20.sem2.games.MiniMaxPlayer;
import inf101.v20.sem2.games.Player;
......@@ -42,7 +42,7 @@ public class TerminalMenu {
break;
case 2:
game = new FourInRow(players);
game = new ConnectFour(players);
break;
default:
throw new IllegalArgumentException("Unexpected value: " + choice);
......
......@@ -10,11 +10,11 @@ import org.junit.jupiter.api.Test;
import inf101.v20.sem2.grid.Location;
import inf101.v20.sem2.terminal.ConsolePlayer;
class FourinRowTest {
class ConnectFourTest {
public final static Player player1 = new ConsolePlayer("X", "Martin");
public final static Player player2 = new ConsolePlayer("O", "Laura");
public FourInRow game;
public ConnectFour game;
public final Random rand = new Random();
@BeforeEach
......@@ -22,8 +22,8 @@ class FourinRowTest {
game = makeGame();
}
public static FourInRow makeGame() {
FourInRow game = new FourInRow();
public static ConnectFour makeGame() {
ConnectFour game = new ConnectFour();
game.addPlayer(player1);
game.addPlayer(player2);
return game;
......
......@@ -12,13 +12,13 @@ public class GameTest {
@Test
void TwoAICanPlay() {
playGame(new TicTacToe(getMiniMaxPlayers()));
playGame(new FourInRow(getMiniMaxPlayers()));
playGame(new ConnectFour(getMiniMaxPlayers()));
}
@Test
void TwoRandomCanPlay() {
playGame(new TicTacToe(getRandomPlayers()));
playGame(new FourInRow(getRandomPlayers()));
playGame(new ConnectFour(getRandomPlayers()));
}
@Test
......@@ -41,7 +41,7 @@ public class GameTest {
int aiwins =0;
int randomwins=0;
for(int i=0; i<100; i++) {
AbstractGame game = new FourInRow(getTwoDifferentAI());
AbstractGame game = new ConnectFour(getTwoDifferentAI());
int result = AICanBeatRandom(game);
if(result == 1)
aiwins++;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment