diff --git a/Battleship/bin/BoatTypes/Boats.class b/Battleship/bin/BoatTypes/Boats.class index 4ca085b..d680291 100644 Binary files a/Battleship/bin/BoatTypes/Boats.class and b/Battleship/bin/BoatTypes/Boats.class differ diff --git a/Battleship/bin/Main.class b/Battleship/bin/Main.class index c4382d4..4cda78f 100644 Binary files a/Battleship/bin/Main.class and b/Battleship/bin/Main.class differ diff --git a/Battleship/bin/Minimax.class b/Battleship/bin/Minimax.class index aedad2c..2994235 100644 Binary files a/Battleship/bin/Minimax.class and b/Battleship/bin/Minimax.class differ diff --git a/Battleship/bin/Player.class b/Battleship/bin/Player.class index 40ea015..d484a63 100644 Binary files a/Battleship/bin/Player.class and b/Battleship/bin/Player.class differ diff --git a/Battleship/bin/TicTacToe.class b/Battleship/bin/TicTacToe.class index 41a80c9..8820dbb 100644 Binary files a/Battleship/bin/TicTacToe.class and b/Battleship/bin/TicTacToe.class differ diff --git a/Battleship/bin/TicTacToeBoard.class b/Battleship/bin/TicTacToeBoard.class index a7341a6..2fc46f7 100644 Binary files a/Battleship/bin/TicTacToeBoard.class and b/Battleship/bin/TicTacToeBoard.class differ diff --git a/Battleship/bin/TicTacToeMainFrame$ButtonHandler.class b/Battleship/bin/TicTacToeMainFrame$ButtonHandler.class new file mode 100644 index 0000000..cdf1cac Binary files /dev/null and b/Battleship/bin/TicTacToeMainFrame$ButtonHandler.class differ diff --git a/Battleship/bin/TicTacToeMainFrame.class b/Battleship/bin/TicTacToeMainFrame.class new file mode 100644 index 0000000..739d1d5 Binary files /dev/null and b/Battleship/bin/TicTacToeMainFrame.class differ diff --git a/Battleship/bin/tickTackToeButton.png b/Battleship/bin/tickTackToeButton.png new file mode 100644 index 0000000..7a687e5 Binary files /dev/null and b/Battleship/bin/tickTackToeButton.png differ diff --git a/Battleship/src/BoatTypes/Boats.java b/Battleship/src/BoatTypes/Boats.java index ef222a1..4ec1793 100644 --- a/Battleship/src/BoatTypes/Boats.java +++ b/Battleship/src/BoatTypes/Boats.java @@ -1,5 +1,4 @@ package BoatTypes; -import java.util.Random; public class Boats { @@ -13,7 +12,7 @@ public class Boats boolean isVertical = false; boolean[] isHit; boolean isSunk; - int health; + public int health; public void setBoatVar(int row, int col, boolean isVertical) @@ -23,12 +22,12 @@ public void setBoatVar(int row, int col, boolean isVertical) this.isVertical = isVertical; if (isVertical) { - this.rowEnd = rowBegin + spaces; + this.rowEnd = rowBegin + (spaces - 1); this.colEnd = colBegin; } else { - this.colEnd = colBegin + spaces; + this.colEnd = colBegin + (spaces - 1); this.rowEnd = rowBegin; } this.isSunk = false; @@ -38,14 +37,16 @@ public boolean isHere(int x, int y) boolean condition = false; if (isVertical) { - if (this.colBegin <= y && y <= this.colEnd ) + // if the boat is vertical the col is constant, and the row varies between the boat rowBegin and boat rowEnd + if (this.colBegin == this.colEnd && this.colEnd == y && this.rowBegin <= x && x <= this.rowEnd) { condition = true; } } else { - if (this.rowBegin <= x && x <= this.rowEnd) + // if the boat is not vertical the row remains the same and the col varies between colBegin and colEnd + if (this.rowBegin == this.rowEnd && this.rowEnd == x && this.colBegin <= y && y <= this.colEnd) { condition = true; } @@ -54,9 +55,7 @@ public boolean isHere(int x, int y) } public void setHit() { - System.out.println("Boat health before: " + this.health); this.health -= 10; - System.out.println("Boat health now: " + this.health); if (health <= 0) { System.out.println("BOAT SUNK"); diff --git a/Battleship/src/Main.java b/Battleship/src/Main.java index 3d7b11c..414d1d9 100644 --- a/Battleship/src/Main.java +++ b/Battleship/src/Main.java @@ -62,7 +62,7 @@ public static void main(String[] args) player1.enemyBoard.printBoard(); System.out.println(); AIPlayer.board.printBoard(); - askForCoordenates(player1.enemyBoard, AIPlayer); + askForCoordinates(player1.enemyBoard, AIPlayer); AIPlay(player1); } } @@ -87,12 +87,16 @@ public static void AIPlay(Player player) col = random.nextInt(size); //check if coordenates hit something - if(player.board.board[row][col] != ' ') + if(player.board.board[row][col] != ' ' && player.board.board[row][col] != '!') { System.out.println("Enemy hit your boat!!"); Boats boat = player.getBoat(row, col); player.board.setHit(row, col); boat.setHit(); + if(boat.health <= 0) + { + player.killBoat(); + } lastoneHit = true; pointHitLast = new int[]{row, col}; } @@ -103,23 +107,27 @@ public static void AIPlay(Player player) } } - public static void askForCoordenates(Board seeableEnemyBoard, Player AIPlayer) + public static void askForCoordinates(Board seeableEnemyBoard, Player AIPlayer) { - //ask for coordenates + //ask for coordinates Scanner in = new Scanner(System.in); System.out.println("Enter row coordenate: "); int row = in.nextInt(); System.out.println("Enter col coordenate: "); int col = in.nextInt(); - //check if coordenates hit something - if(AIPlayer.board.board[row][col] != ' ') + //check if coordinates hit something + if(AIPlayer.board.board[row][col] != ' ' && AIPlayer.board.board[row][col] != '!') { System.out.println("You got a hit!!"); seeableEnemyBoard.setHit(row, col); Boats boat = AIPlayer.getBoat(row, col); AIPlayer.board.setHit(row, col); boat.setHit(); + if (boat.health <= 0) + { + AIPlayer.killBoat(); + } } else { diff --git a/Battleship/src/Move.java b/Battleship/src/Move.java new file mode 100644 index 0000000..0f6c471 --- /dev/null +++ b/Battleship/src/Move.java @@ -0,0 +1,11 @@ + +public class Move +{ + int row; + int col; + public Move(int row, int col) + { + this.row = row; + this.col = col; + } +} diff --git a/Battleship/src/Player.java b/Battleship/src/Player.java index 8bf648f..3c43caf 100644 --- a/Battleship/src/Player.java +++ b/Battleship/src/Player.java @@ -14,6 +14,8 @@ public class Player Board board; Board enemyBoard; int boardSize; + int boatsAlive = 10; + public Player(int size) { @@ -31,7 +33,15 @@ public Player(int size) boats[9] = new Submarine(); this.boardSize = size; } - + public void killBoat() + { + boatsAlive--; + if (boatsAlive <= 0) + { + System.out.println("GAME OVER!"); + System.exit(0); + } + } public void setBoard() { for(int i = 0; i < boats.length; i++) @@ -93,7 +103,7 @@ public void setRandomBoard() public Boats getBoat(int row, int col) { - Boats boatFound = new Boats(); + Boats boatFound = null; for (int i = 0; i < boats.length; i++) { if (boats[i].isHere(row, col)) diff --git a/TicTacToe/Minimax.java b/TicTacToe/Minimax.java index 7a72b15..4e2c9dd 100644 --- a/TicTacToe/Minimax.java +++ b/TicTacToe/Minimax.java @@ -1,145 +1,160 @@ -public final class Minimax +import java.util.ArrayList; + +public class Minimax { - private static Action bestActionForX; - private static Action bestActionForO; - private static Action bufferAction; - private static int max = -10; - private static int min = 10; - private static TicTacToeBoard bufferBoard; - - private Minimax() + // Player is AIPlayer and opponent is Human Player + private char playerAI, opponent; + // private TicTacToeBoard gameBoard; + public Action bestChoice; + + public int endGameScore(TicTacToeBoard gameBoard) { - // do not instantiate + System.out.println("Reached End Game Player who won: " + gameBoard.getPlayer()); + if (gameBoard.whoWon() == playerAI) + { + System.out.println("DETECTED AI WON"); + return 10; + } + else if (gameBoard.whoWon() == opponent) + { + System.out.println("DETECTED OPPONENT WON"); + return -10; + } + else + { + System.out.println("Detected Draaaaaww!"); + return 0; + } } - public static Action minimax(TicTacToeBoard board) - { - maxValue(board); - //return bestActionForO; - return bufferAction; - - } - - public static void clean() + public Action getBestChoice() { - bestActionForX = null; - bestActionForO = null; - //max = -10; - //min = 10; + return this.bestChoice; } - public static int maxValue(TicTacToeBoard board) + + public Minimax(TicTacToeBoard board, char playerAI) { - // if its a terminal state - if(board.isGameOver()) + this.playerAI = playerAI; + if (playerAI == 'O') { - - char winner = board.whoWon(); - if (winner == 'O') + opponent = 'X'; + } else + { + opponent = 'O'; + } + // making new board + char[][] boardArrayChar = new char[3][3]; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) { - return 1; + boardArrayChar[i][j] = board.getArrayBoard()[i][j]; } - else if (winner == 'X') + } + TicTacToeBoard receivedBoard = new TicTacToeBoard(boardArrayChar); + minimax(receivedBoard); + } + + public int minimax(TicTacToeBoard boardR) + { + // making new board + char[][] boardArrayChar = new char[3][3]; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) { - return -1; + boardArrayChar[i][j] = boardR.getArrayBoard()[i][j]; } - else + } + TicTacToeBoard board = new TicTacToeBoard(boardArrayChar); + if (board.isGameOver()) + { + return endGameScore(board); + } + + ArrayList scores = new ArrayList(); + // ArrayList storeMoves = new ArrayList(); + + Action[] moves = board.possiblePlays(); + // making new board + char[][] boardReceivedCharArray = new char[3][3]; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) { - return 0; + boardReceivedCharArray[i][j] = board.getArrayBoard()[i][j]; } } - Action[] actions = board.possiblePlays(); - - for(int i = 0; i < actions.length; i++) + // populate array scores + for (int i = 0; i < moves.length; i++) { - char[][] bb = new char[3][3]; - // make copy of array - for(int j=0;j max) + { + max = (int) scoresArray[i]; + maxScoreIsAt = i; + bestChoice = moves[i]; + } } - } - - - Action[] actions = board.possiblePlays(); - char[][] bb = new char[3][3]; - // make copy of array - - for(int i = 0; i < actions.length; i++) + return (int) scoresArray[maxScoreIsAt]; + } + else { - for(int j = 0;j < board.getArrayBoard().length; j++) + System.out.println("Decided to do MIN, Player: " + player); + // Do min + int minScoreIsAt = 0; + int min = 10; + Integer[] scoresArray = scores.toArray(new Integer[scores.size()]); + for (int i = 0; i < scoresArray.length; i++) { - for (int k = 0;k < board.getArrayBoard()[j].length;k++) + if ((int) scoresArray[i] < min) { - bb[j][k] = board.getArrayBoard()[j][k]; + min = (int) scoresArray[i]; + minScoreIsAt = i; + bestChoice = moves[i]; } } - TicTacToeBoard possibleBoard = new TicTacToeBoard(board.getTurn(), bb); - possibleBoard.setBoard('X', actions[i].getAction()[0], actions[i].getAction()[1]); - if (min > maxValue(possibleBoard)) - { - min = maxValue(possibleBoard); - bestActionForX = actions[i]; - } - } - //System.out.println("MAx in MIN is: " + max + " Min in Min is: " + min); - //board.printBoard(); - if(!board.isGameOver()) - { - clean(); + return (int) scoresArray[minScoreIsAt]; } - return min; } - -} +} \ No newline at end of file diff --git a/TicTacToe/TicTacToe.java b/TicTacToe/TicTacToe.java index 9397df1..95b1ff2 100644 --- a/TicTacToe/TicTacToe.java +++ b/TicTacToe/TicTacToe.java @@ -1,63 +1,114 @@ import java.util.Scanner; +import javax.swing.JFrame; +import javax.swing.JOptionPane; + + public class TicTacToe { - public static void main(String[] args) + public static void makeNewGame(Game game, TicTacToeMainFrame f) { - Game game = new Game(); - - - Scanner in = new Scanner(System.in); - // While game isn't over + char player = f.getPlayer(); + char opponent; + opponent = player == 'X' ? 'O' : 'X'; while(!game.getBoard().isGameOver()) { - System.out.println("Enter row of play"); - int row = in.nextInt(); - System.out.println("Enter col of play"); - int col = in.nextInt(); - game.makeMove(row, col); - game.getBoard().printBoard(); - //if game still not over - /* - char [][] testeBoardChar = new char[][] + if (player == 'X') { - {'X', ' ', 'X'}, - {' ', 'O', ' '}, - {'O', ' ', 'X'}, - }; - - */ - //Board teste = new Board(testeBoardChar); - //char a = teste.whoWon(); - //System.out.println("venceu !!! " + a); - Action Move = Minimax.minimax(game.getBoard()); - game.makeMove(Move.getAction()[0], Move.getAction()[1]); - game.getBoard().printBoard(); - //System.out.println("Ação " + Move.getAction()[0] + Move.getAction()[1]); - /* - if(!game.getBoard().isGameOver()) + while (!f.isActionSet()) + { + System.out.println("Waiting for click"); + //waiting for action + } + + Action newAction = f.getAction(); + f.clearAction(); + game.makeMove(newAction.getAction()[0], newAction.getAction()[1]); + game.getBoard().printBoard(); + + if (game.getBoard().isGameOver()) + { + break; + } + + Minimax AI = new Minimax(game.getBoard(), 'O'); + Action Move = AI.getBestChoice(); + game.makeMove(Move.getAction()[0], Move.getAction()[1]); + f.setButtonText(Move.getAction()[0], Move.getAction()[1]); + game.getBoard().printBoard(); + } + else { - char[][] board = new char[3][3]; - for(int i=0;i 2 || col > 2 || this.board[row][col] != ' ') + if (row > 2 || col > 2 || this.board[row][col] != ' ') { System.out.println("Ilegal play, try again"); return false; - } - else + } else { return true; } } + public boolean isGameOver() { - - for(int i=0;i<3;i++) + + for (int i = 0; i < 3; i++) { // check vertical lines - if(board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][1] != ' ') + if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][1] != ' ') { - //System.out.println("Game won by: " + board[i][1]); + // System.out.println("Game won by: " + board[i][1]); return true; } - //check horizontal lines - if(board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') + // check horizontal lines + if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') { - //System.out.println("Game won by: " + board[0][i]); + // System.out.println("Game won by: " + board[0][i]); return true; } } - //check diagonals - if(((board[0][0] == board[1][1] && board[1][1] == board[2][2]) || - (board[0][2] == board[1][1] && board[1][1] == board[2][0])) && board[1][1] != ' ') + // check diagonals + if (((board[0][0] == board[1][1] && board[1][1] == board[2][2]) + || (board[0][2] == board[1][1] && board[1][1] == board[2][0])) && board[1][1] != ' ') { - //System.out.println("Game won by " + board[1][1]); + // System.out.println("Game won by " + board[1][1]); return true; } if (this.getTurn() >= 9) { - //System.out.println("Game draw by stalemate"); + // System.out.println("Game draw by stalemate"); return true; - } - else + } else { return false; } - + } - - //make it simple CLEAN THE CODE + + // make it simple CLEAN THE CODE public char whoWon() { + char a = ' '; if (this.isGameOver()) { - for(int i=0;i<3;i++) + for (int i = 0; i < 3; i++) { // check horizontal lines - if(board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][1] != ' ') + if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][1] != ' ') { - System.out.println("Game won by: " + board[i][1]); return board[i][1]; } - //check vertical lines - if(board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') + // check vertical lines + else if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') { - System.out.println("Game won by: " + board[0][i]); return board[0][i]; } } - //check diagonals - if(((board[0][0] == board[1][1] && board[1][1] == board[2][2]) || - (board[0][2] == board[1][1] && board[1][1] == board[2][0])) && board[1][1] != ' ') + // check diagonals + if (((board[0][0] == board[1][1] && board[1][1] == board[2][2]) + || (board[0][2] == board[1][1] && board[1][1] == board[2][0])) && board[1][1] != ' ') { - System.out.println("Game won by " + board[1][1]); return board[1][1]; - } - else + } else { - System.out.println("Game draw"); - return 'd'; + return a; } - } - else + } else { - System.out.println("Game not over"); - return 'n'; + System.exit(2); + return a; } } - + public Action[] possiblePlays() { ArrayList possiblePlays = new ArrayList(); @@ -171,10 +177,11 @@ public Action[] possiblePlays() possiblePlays.trimToSize(); return possiblePlays.toArray(new Action[possiblePlays.size()]); } + public int getTurn() { - int count =0; - for(int i = 0; i < 3; i++) + int count = 0; + for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { @@ -186,16 +193,18 @@ public int getTurn() } return count; } + public char getPlayer() { + int turn = getTurn(); if (turn % 2 == 0) { - return 'X'; - } - else + return 'X'; + } else { return 'O'; } - } + + } } diff --git a/TicTacToe/TicTacToeMainFrame.java b/TicTacToe/TicTacToeMainFrame.java new file mode 100644 index 0000000..dc6f441 --- /dev/null +++ b/TicTacToe/TicTacToeMainFrame.java @@ -0,0 +1,158 @@ +import java.awt.Color; +import java.awt.Font; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JOptionPane; +import javax.swing.JPanel; + +public class TicTacToeMainFrame extends JFrame +{ + JButton[][] buttons = new JButton[3][3]; + private GridLayout gridLayout; + private char player, opponent; + private Action lastAction; + private int turn = 0; + public TicTacToeMainFrame() + { + super("TicTacToe Game by Damian Vaz"); + drawFrame(); + + } + public void clearFrame() + { + this.dispose(); + } + public void drawFrame() + { + Object[] options1 = {"I want to be Player X", "I want to be Player O"}; + + int mode = JOptionPane.showOptionDialog(null, "Choose your Player", "Player Selection", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options1, null); + if (mode == JOptionPane.YES_OPTION) + { + player = 'X'; + } + else + { + player = 'O'; + } + + opponent = player == 'X' ? 'O' : 'X'; + gridLayout = new GridLayout(3, 3, 5, 5); + getContentPane().setBackground(Color.BLACK); + setLayout(gridLayout); + ButtonHandler buttonHandler = new ButtonHandler(); + for (int i = 0; i < buttons.length; i++) + { + for (int j = 0; j < buttons[i].length; j++) + { + buttons[i][j] = new JButton(); + buttons[i][j].setBackground(Color.WHITE); + buttons[i][j].addActionListener(buttonHandler); + add(buttons[i][j]); + } + } + this.revalidate(); + } + + public void setButtonText(int i, int j) + { + buttons[i][j].setText("" + opponent); + buttons[i][j].setFont(new Font("Arial", Font.PLAIN, 73)); + turn++; + } + public Action getAction() + { + return this.lastAction; + } + public boolean isActionSet() + { + if(lastAction != null) + { + return true; + } + else + { + return false; + } + } + public void clearAction() + { + lastAction = null; + } + public char getPlayer() + { + return this.player; + } + private class ButtonHandler implements ActionListener + { + + @Override + public void actionPerformed(ActionEvent e) + { + if (player == 'X') + { + int rest = turn % 2; + if (rest != 0) + { + JOptionPane.showMessageDialog(null, "It's still my turn! \n be patient I'm thinking", "Hold On", JOptionPane.ERROR_MESSAGE); + } + else + { + turn++; + int x = 0; + int y = 0; + JButton buttonPressed = (JButton) e.getSource(); + for (int i = 0; i < buttons.length; i++) + { + for (int j = 0; j < buttons[i].length; j++) + { + if (buttonPressed.equals(buttons[i][j])) + { + x = i; + y = j; + } + } + } + lastAction = new Action(x, y); + buttonPressed.setFont(new Font("Arial", Font.PLAIN, 73)); + buttonPressed.setText("" + player); + } + } + else + { + int rest = turn % 2; + if (rest == 0) + { + JOptionPane.showMessageDialog(null, "It's still my turn! \n be patient I'm thinking", "Hold On", JOptionPane.ERROR_MESSAGE); + } + else + { + turn++; + int x = 0; + int y = 0; + JButton buttonPressed = (JButton) e.getSource(); + for (int i = 0; i < buttons.length; i++) + { + for (int j = 0; j < buttons[i].length; j++) + { + if (buttonPressed.equals(buttons[i][j])) + { + x = i; + y = j; + } + } + } + lastAction = new Action(x, y); + buttonPressed.setFont(new Font("Arial", Font.PLAIN, 73)); + buttonPressed.setText("" + player); + } + } + + } + + } +}