Commit 60771cf7 authored by mva021's avatar mva021
Browse files

Replaced game with new solution

parent 357cb687
package inf101.v20.sem2.GUI;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.List;
import javax.swing.JPanel;
import inf101.v20.sem2.grid.Grid;
import inf101.v20.sem2.grid.Location;
import inf101.v20.sem2.games.IGame;
import inf101.v20.sem2.games.Player;
public class ClickableGrid extends MouseAdapter {
private JPanel pane;
private Grid<GamePanel> clickablePanels; //clickable grid for user input
private IGame game;
HashMap<Player,Color> color;
public ClickableGrid(IGame game,List<Color> colors) {
this.game = game;
//assign colors to the players
setColors(game, colors);
//create clickable panels
pane = new JPanel();
int rows = game.getGameBoard().numRows();
int cols = game.getGameBoard().numCols();
clickablePanels = new Grid<GamePanel>(rows, cols, null);
pane.setLayout(new GridLayout(rows, cols));
makeClickablePanels();
//set gui options
pane.setRequestFocusEnabled(true);
pane.requestFocus();
pane.setVisible(true);
pane.validate();
}
private void setColors(IGame game, List<Color> colors) {
color = new HashMap<Player, Color>();
for(Player p : game.players()) {
color.put(p, colors.get(color.size()));
}
color.put(null, Color.WHITE);
}
/**
* Should be called after a click to update the UI to reflect the current game state
*/
public void updateGui() {
for(Location loc : game.getGameBoard().locations()) {
Color color = getColor(game.getGameBoard().get(loc));
clickablePanels.get(loc).setColor(color);
}
pane.updateUI();
}
protected Color getColor(Player player) {
return color.get(player);
}
/**
* Makes a grid of same size as the one in game and fills it with clickable panels
* @return
*/
private void makeClickablePanels() {
for(Location loc : game.getGameBoard().locations()) {
GamePanel pan = new GamePanel(this);
clickablePanels.set(loc, pan);
pane.add(pan);
}
}
/**
* This is what happens when the mouse clicks on one of the squares of the grid.
*/
public void mousePressed(MouseEvent me){
if(clickablePanels.contains(me.getSource())) {
Location loc = clickablePanels.locationOf(me.getSource());
System.err.println("Making move: "+loc);
game.makeGuiMove(loc);
}
else
System.err.println("Clicked on wrong thing: "+me.getSource());
}
public Component getPanel() {
return pane;
}
}
\ No newline at end of file
package inf101.v20.sem2.GUI;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import inf101.v20.sem2.games.Game;
import inf101.v20.sem2.games.Player;
import inf101.v20.sem2.games.TicTacToe;
public class GUIMain {
public static void main(String[] args) {
ArrayList<Player> players = new ArrayList<Player>();
players.add(new GuiPlayer(TicTacToe.piece1));
players.add(new GuiPlayer(TicTacToe.piece2));
Game game = new TicTacToe(players);
MNKGameGUI gameGUI = new MNKGameGUI(game);
game.setGui(gameGUI);
GameGUI gui = new GameGUI(gameGUI);
}
}
\ No newline at end of file
package inf101.v20.sem2.mnkgames.GUI;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import inf101.v20.sem2.mnkgames.MNKGame;
import inf101.v20.sem2.twoPlayerGame.TwoPlayerGame;
import inf101.v20.sem2.twoPlayerGame.player.HumanPlayer;
/**
*
* @author Anna Eilertsen
*/
public class MNKGameGUI extends JPanel implements ActionListener{
private static final long serialVersionUID = 8755882090377973497L;
/**
s * Initializes a JFrame in which we place the game
* @param gameSupplier A lambda function that provides new games
*/
public static void run(GameSupplier gameSupplier) {
JFrame f = new JFrame("mnkGame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MNKGameGUI ap = new MNKGameGUI(gameSupplier);
ap.collectPlayerInfo();
ap.initialize();
f.add("Center", ap);
f.pack();
f.setVisible(true);
}
// Fields
private JButton playConnectFourButton;
private JButton playTicTacToeButton;
private JLabel statusMessage;
private GameSupplier gameSupplier;
private TwoPlayerGame<MNKGame> game;
private GUIBoard board;
public MNKGameGUI(GameSupplier gameSupplier) {
this.gameSupplier = gameSupplier;
//initialize to default values
game = new TwoPlayerGame<>(gameSupplier.getTicTacToeGame());
}
/*
* (non-Javadoc)
*
* @see
* java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
* Called whenever a button is pressed
*/
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == playConnectFourButton) {
reset(gameSupplier.getConnectFourGame());
}
if(e.getSource() == playTicTacToeButton) {
reset(gameSupplier.getTicTacToeGame());
}
}
private void reset(MNKGame newGame) {
this.game = game.newGame(newGame);
updateMessage();
removeAll();
initialize();
this.updateUI();
}
public void registerClick(int x, int y) {
try {
game.takeTurnforHumanPlayer(x, y);
}
catch(Exception e){
//GUI ERROR HANDLING
}
board.updateToGame(game);
updateMessage();
MNKGameGUI.super.updateUI();
if(!game.hasPossibleMoves()) {
stop();
}
}
/**
* Helper method to control the status message
*/
private void updateMessage() {
if (game.hasWinner())
statusMessage.setText("The game is over! The winner is " + game.winner());
else if(game.hasPossibleMoves()) {
statusMessage.setText("Next move from " + game.currentPlayer());
}else{
statusMessage.setText("The game is over! No one won.");
}
}
/**
* Should be called once a game stops
*/
private void stop() {
board.makeAllUnclickable();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100*game.height(), 100*game.width());
}
/**
* Sets up the GUI.
*/
public void initialize() {
setLayout(new BorderLayout());
board = new GUIBoard(this, game);
add(board);
JPanel buttons = createButtonPanel();
add(buttons, BorderLayout.SOUTH);
add(statusMessage, BorderLayout.NORTH);
validate();
}
/**
* Helper method that creates the button panel
*/
private JPanel createButtonPanel() {
playTicTacToeButton = new JButton();
playTicTacToeButton.addActionListener(this);
playTicTacToeButton.setText("Tic-Tac-Toe");
playConnectFourButton = new JButton();
playConnectFourButton.addActionListener(this);
playConnectFourButton.setText("Connect Four");
statusMessage = new JLabel();
statusMessage.setText("Welcome to this game! " + game.player1() + " begins.");
JPanel buttons = new JPanel();
buttons.setLayout(new BorderLayout());
buttons.add(playConnectFourButton, BorderLayout.WEST);
buttons.add(playTicTacToeButton, BorderLayout.EAST);
return buttons;
}
/**
* Helper method to collect player info
*/
private void collectPlayerInfo() {
boolean multiplayer = promptMultiplayer();
game.setHumanPlayer1(HumanPlayer.promptPlayerName("Player 1"));
if(multiplayer) {
game.setHumanPlayer2(HumanPlayer.promptPlayerName("Player 2"));
}
else {
game.setAIPlayer2();
}
}
/**
* Helper method that prompts for multiplayer or not
* @return true if multiplayer is selected, false otherwise
*/
private boolean promptMultiplayer() {
Object[] possibilities = {"Multiplayer", "Single Player (against AI)"};
String message = "Welcome:\n" + "Select one or two players";
String s = (String)JOptionPane.showInputDialog(null,message,"MKGame StartUp",
JOptionPane.PLAIN_MESSAGE,null,possibilities,null);
//If a string was returned, say so.
if ((s != null) && (s.length() > 0)) {
System.out.println("Received " + s);
}
return s.charAt(0)=='M';
}
}
\ No newline at end of file
package inf101.v20.sem2.GUI;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import inf101.v20.sem2.games.FourinRow;
import inf101.v20.sem2.games.Player;
import inf101.v20.sem2.games.TicTacToe;
public class GameGUI implements ActionListener{
private JButton playConnectFourButton; //Button to start new 4 in row game
private JButton playTicTacToeButton; //Button to start new TicTacToe game
private JFrame frame;
private MNKGameGUI gameBoard;
public GameGUI(MNKGameGUI gameBoard) {
this.gameBoard = gameBoard;
JPanel buttons = createButtonPanel();
//make new main window for the game
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(buttons, BorderLayout.SOUTH);
drawGameBoard();
}
private void drawGameBoard() {
//add the clickable grid panel
frame.add("Center", gameBoard);
frame.pack();
frame.setVisible(true);
}
/**
* Helper method that creates the button panel
*/
private JPanel createButtonPanel() {
playTicTacToeButton = new JButton();
playTicTacToeButton.addActionListener(this);
playTicTacToeButton.setText("Tic-Tac-Toe");
playConnectFourButton = new JButton();
playConnectFourButton.addActionListener(this);
playConnectFourButton.setText("Connect Four");
JPanel buttons = new JPanel();
buttons.setLayout(new BorderLayout());
buttons.add(playConnectFourButton, BorderLayout.WEST);
buttons.add(playTicTacToeButton, BorderLayout.EAST);
return buttons;
}
/*
* (non-Javadoc)
*
* @see
* java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
* Called whenever a button is pressed
*/
@Override
public void actionPerformed(ActionEvent e) {
Iterable<Player> players = getPlayers();
if(e.getSource() == playConnectFourButton) {
gameBoard.reset(new FourinRow(players));
}
if(e.getSource() == playTicTacToeButton) {
gameBoard.reset(new TicTacToe(players));
}
drawGameBoard();
}
private Iterable<Player> getPlayers() {
ArrayList<Player> players = new ArrayList<Player>();
//add player1
players.add(new GuiPlayer(TicTacToe.piece1));
//add player2
if(promptMultiplayer()) {
//make AI
}else {
players.add(new GuiPlayer(TicTacToe.piece2));
}
return players;
}
/**
* Helper method that prompts for multiplayer or not
* @return true if multiplayer is selected, false otherwise
*/
private boolean promptMultiplayer() {
Object[] possibilities = {"Multiplayer", "Single Player (against AI)"};
String s = (String)JOptionPane.showInputDialog(
null,
"Welcome:\n"
+ "Select one or two players",
"MKGame StartUp",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
null);
//If a string was returned, say so.
if ((s != null) && (s.length() > 0)) {
System.out.println("Received " + s);
}
return s.charAt(0)=='M';
}
}
package inf101.v20.sem2.GUI;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
/**
* A class for clickable panels with x,y-coordinates
*
* @author anna
*
*/
public class GamePanel extends JPanel{
private Color color;
private static final long serialVersionUID = 1L;
public GamePanel(MouseListener listener) {
this.color = Color.WHITE;
//use methods in JPanel to set initial style
setEnabled(true);
setBorder(BorderFactory.createLineBorder(Color.GRAY));
//add mouse listener which calls click method
addMouseListener(listener);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(3, 3);
}
@Override
public Color getBackground() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
\ No newline at end of file
package inf101.v20.sem2.GUI;
import javax.swing.JOptionPane;
import inf101.v20.sem2.grid.Location;
import inf101.v20.sem2.games.AbstractPlayer;
import inf101.v20.sem2.games.IGame;
import inf101.v20.sem2.games.Player;
public class GuiPlayer extends AbstractPlayer {
public GuiPlayer(String piece, String name) {
super(piece, name);
}
public GuiPlayer(String piece) {
super(piece, getPlayerName(piece));
}
@Override
public Location getMove(IGame game) {
throw new IllegalStateException("This method should not be called.");
}
public static String getPlayerName(String piece) {
String name = null;
while(!Player.isValidName(name)) {
name = JOptionPane.showInputDialog("Player "+piece+". Type in your name.");
}
return name;
}
}
package inf101.v20.sem2.GUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.Arrays;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import inf101.v20.sem2.games.Game;
/**
*
* @author Anna Eilertsen
*/
public class MNKGameGUI extends JPanel{
private static final long serialVersionUID = 8755882090377973497L;
// Fields
private Game game; //the game logic
private ClickableGrid clickablePanels; //clickable grid for user input
private JLabel statusMessage; //field for displaying message to user
public MNKGameGUI(Game game) {
this.game = game;
statusMessage = new JLabel();
statusMessage.setText("Welcome to this game! " + game.getCurrentPlayerName() + " begins.");
clickablePanels = new ClickableGrid(game, MNKGameGUI.getColors());
initialize();
}
/**
* Sets up the GUI.
*/
private void initialize() {
setLayout(new BorderLayout());
add("North", statusMessage);
add("Center", clickablePanels.getPanel());
setVisible(true);
validate();
}
public void reset(Game newGame) {
this.game = newGame;
//updateMessage();
removeAll();
initialize();
this.updateUI();
}
public void updateMessage(String message) {
statusMessage.setText(message);
}
/**
* Maps from Piece values to colors
*
* @param pieceAt The piece to be drawn
* @return The color that this GUI implementation associates with the provided piece
*/
protected static List<Color> getColors() {
Color[] colors = {Color.BLUE,Color.RED};
return Arrays.asList(colors);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100*game.getGameBoard().numRows(), 100*game.getGameBoard().numRows());
}
@Override
public void updateUI() {
if(clickablePanels!=null)
clickablePanels.updateGui();
super.updateUI();
}