public GUILogi5Board(Game game) { this.game = game; dim = game.getDimensions()[0]; setLayout(new GridLayout(dim, dim)); spaces = new JButton[dim][dim]; for (int i = 0; i < spaces.length; i++) { for (int j = 0; j < spaces[i].length; j++) { Space otherSpace = game.getSpaceAt(i, j); GUILogi5Space spacey = new GUILogi5Space(i, j, spaceSize); int[] loc = spacey.getDims(); spacey.setSpace(game.getSpaceAt(loc[0], loc[1])); spaces[i][j] = spacey; add(spacey); } } setBorder(BorderFactory.createLineBorder(Color.black)); setPreferredSize(new Dimension(spaceSize * spaces.length, spaceSize * spaces[0].length)); setMaximumSize(new Dimension(spaceSize * spaces.length, spaceSize * spaces[0].length)); int num = 255; Color[] colors = new Color[6]; for (int i = 0; i < colors.length; i++) { colors[i] = new Color(num, num, num, 220); num -= 30; } possibleColors = colors; }
private void paintMenu(Graphics2D g2d) { if (showMenu == true) { if (c.getPoints() > c.getHighscore().getLowestPointsInTable() && c.getLifes() == 0) { menu.setState(4); } menu.draw(g2d); } }
/* Create and show the graphical user interface. */ private static void createAndShowGUI() { JFrame frame = new JFrame("My Collapsing Puzzle"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Game game = new Game(); frame.setJMenuBar(game.createMenuBar()); frame.setContentPane(game.createContentPane()); frame.setSize(game.getGameSize()); frame.setVisible(true); }
public void play(int lv) { jl.setText("Level " + level); Game game = new Game(lv); // An object representing the game View view = new View(game); // An object representing the view of the game game.newGame(); view.print(); gameBoardPanel = view.mainPanel; ButtonPanel buttonPanel = new ButtonPanel(game); container.add(buttonPanel, BorderLayout.EAST); container.add(gameBoardPanel, BorderLayout.WEST); mainFrame.pack(); // Main game loop while (true) { view.print(); gameBoardPanel = view.mainPanel; // Win/lose conditions if (game.isWin()) { view.print(); gameBoardPanel = view.mainPanel; int choice; choice = JOptionPane.showConfirmDialog(null, "You win!", "", JOptionPane.OK_OPTION); if (choice == JOptionPane.OK_OPTION) { level++; mainFrame.remove(buttonPanel); mainFrame.remove(gameBoardPanel); play(level); } } if (game.isLose()) { view.print(); gameBoardPanel = view.mainPanel; int choice; choice = JOptionPane.showConfirmDialog( null, "You lose!", "Would you like to play again?", JOptionPane.YES_NO_OPTION); if (choice == JOptionPane.YES_OPTION) { level = 1; mainFrame.remove(buttonPanel); mainFrame.remove(gameBoardPanel); play(level); } else { System.exit(0); } } } }
// --------------------------------createAndShowGUI----------------------------- public static void createAndShowGUI() { Game thisGame = new Game(); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { } JFrame.setDefaultLookAndFeelDecorated(true); thisGame.frame = new JFrame("Tactics"); thisGame.oConn = new SocketManager(); thisGame.scrBounds = thisGame.frame.getGraphicsConfiguration().getBounds(); thisGame.frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); thisGame.frame.setDefaultLookAndFeelDecorated(true); thisGame.frame.addWindowListener(thisGame); thisGame.frame.setVisible(true); }
public void setUpGroups() { this.groups = game.getGroups(); Color[] groupToColor = new Color[groups.length]; for (int i = 0; i < groupToColor.length; i++) { groupToColor[i] = possibleColors[i % possibleColors.length]; } for (int i = 0; i < groupToColor.length; i++) { Group[] conflictingGroups; conflictingGroups = conflicting(i, groupToColor); int noOfTimes = 0; while (conflictingGroups.length != 0) { int offset; if (noOfTimes > 5) offset = 3; else offset = 2; groupToColor[conflictingGroups[0].groupIDX] = possibleColors[(conflictingGroups[0].groupIDX + offset) % possibleColors.length]; conflictingGroups = conflicting(i, groupToColor); noOfTimes++; } } for (int i = 0; i < groups.length; i++) { Space[] groupSpaces = groups[i].getSpaces(); for (int j = 0; j < groupSpaces.length; j++) { Space current = groupSpaces[j]; GUILogi5Space currentSpace = (GUILogi5Space) (spaces[current.getX()][current.getY()]); currentSpace.setColor(groupToColor[i]); } } }
/** * Create a new board * * @param c */ public Board(Game c) { // add leap motion controller leapAdapter = new SensorAdapter(c); sensorListener = new SensorListener(leapAdapter); shootListener = new ShootListener(leapAdapter); leapController = new Controller(); leapController.addListener(sensorListener); leapController.addListener(shootListener); addKeyListener(new KeyboardAdapter(c, this)); this.menu = new Menu(this, c); setFocusable(true); setDoubleBuffered(true); this.c = c; c.setPlayer(ItemFactory.createPlayer()); c.setBackground(ItemFactory.createBackground()); }
private void paintItems(Graphics2D g2d) { for (Item item : c.getAllStuff()) { if (item != null) { if (item.flicker()) { g2d.drawImage( item.getImage(), item.getPosition().getX(), item.getPosition().getY(), this); } } } }
@Override public void mouseClicked(MouseEvent event) { Object obj = event.getSource(); if (obj instanceof JButton) { JButton clickedButton = (JButton) obj; if (clickedButton.equals(one)) { this.clearPanel(); Game next = new Game(this.frame, 1, this.bc); next.setBounds(0, 0, this.getWidth(), this.getHeight()); this.frame.setContentPane(next); this.revalidate(); this.repaint(); } if (clickedButton.equals(two)) { this.clearPanel(); Game next = new Game(this.frame, 2, this.bc); next.setBounds(0, 0, this.getWidth(), this.getHeight()); this.frame.setContentPane(next); this.revalidate(); this.repaint(); } if (clickedButton.equals(three)) { this.clearPanel(); Game next = new Game(this.frame, 3, this.bc); next.setBounds(0, 0, this.frame.getWidth(), this.frame.getHeight()); this.frame.setContentPane(next); this.revalidate(); this.repaint(); } if (clickedButton.equals(four)) { this.clearPanel(); Game next = new Game(this.frame, 4, this.bc); next.setBounds(0, 0, this.frame.getWidth(), this.frame.getHeight()); // this.frame.setContentPane(next); this.frame.add(next); this.revalidate(); this.repaint(); } if (clickedButton.equals(back)) { this.clearPanel(); HomePanel next = new HomePanel(this.frame, bc); next.setBounds(0, 0, this.getWidth(), this.getHeight()); // this.frame.setContentPane(next); this.frame.add(next); this.revalidate(); this.repaint(); } } }
private void paintLifes(Graphics2D g2d) { int lifes = c.getLifes(); for (int i = 0; i < lifes; i++) { ImageIcon imageIcon = new ImageIcon( ItemFactory.class.getResource(Config.getImagePath() + Level.getLevel().getLife())); Life life = ItemFactory.createLife( Config.getBoardDimension().getLength() - (2 + i) * imageIcon.getIconWidth(), 0, imageIcon); g2d.drawImage(life.getImage(), life.getPosition().getX(), life.getPosition().getY(), this); } }
@Override public void paint(Graphics g) { super.paint(g); // menu on game over if (c.getLifes() < 1) { showMenu = true; } this.paintBackground((Graphics2D) g); this.paintItems((Graphics2D) g); this.paintLifes((Graphics2D) g); this.paintScore((Graphics2D) g); this.paintMenu((Graphics2D) g); Toolkit.getDefaultToolkit().sync(); g.dispose(); }
private Group[] conflicting(int ish, Color[] groupToColor) { ArrayList<Group> touchers = new ArrayList<Group>(); Group g = groups[ish]; Space[] gSpaces = g.getSpaces(); for (int i = 0; i < gSpaces.length; i++) { Space current = gSpaces[i]; for (int k = -1; k < 2; k++) { int curSX = current.getX(); int curSY = current.getY(); curSX += k; curSY += k; if (curSX < 0) { curSX = 0; } if (curSX > spaces.length - 1) { curSX = spaces.length - 1; } if (curSY < 0) { curSY = 0; } if (curSY > spaces.length - 1) { curSY = spaces.length - 1; } Space[] comparison = new Space[2]; comparison[0] = game.getSpaceAt(curSX, current.getY()); comparison[1] = game.getSpaceAt(current.getX(), curSY); int mod = groupToColor.length; for (int j = 0; j < comparison.length; j++) { if ((comparison[j].getGroup().groupIDX != g.groupIDX) && groupToColor[comparison[j].getGroup().groupIDX % mod] == groupToColor[current.getGroup().groupIDX % mod] && !touchers.contains(comparison[j].getGroup())) { touchers.add(comparison[j].getGroup()); } } } } return touchers.toArray(new Group[touchers.size()]); }
@Override public void startTheGame() { gameType = ""; try { // receive ping from server gameType = mainServer.readLine(); } catch (IOException ex) { System.err.println("Could not communicate with server: " + ex); Global.onException(); } super.startTheGame(); for (FixedTimer timer : timers) timer.start(); startMarquee(getGameType(), 1000); startMarquee("3"); startMarquee("2"); startMarquee("1"); // receive ping that the game is starting try { // ping back that client is ready mainServer.println(); mainServer.flush(); // wait for if server is ready mainServer.readLine(); } catch (IOException ex) { System.err.println("Could not communicate with server: " + ex); Global.onException(); } turnOnEndGameReceiver(); startMarquee("START!"); }
private void paintBackground(Graphics2D g2d) { int x = c.getBackground().getPosition().getX(); int y = c.getBackground().getPosition().getY(); g2d.drawImage( c.getBackground().getImage(), x - c.getBackground().getDimension().getLength(), y, Config.getBoardDimension().getLength(), Config.getBoardDimension().getHeight(), this); g2d.drawImage( c.getBackground().getImage(), x, y, Config.getBoardDimension().getLength(), Config.getBoardDimension().getHeight(), this); g2d.drawImage( c.getBackground().getImage(), x + c.getBackground().getDimension().getLength(), y, Config.getBoardDimension().getLength(), Config.getBoardDimension().getHeight(), this); if (Config.getForeground().compareTo("") == 0) { ImageIcon foreground = new ImageIcon( this.getClass().getResource(Config.getImagePath() + Config.getForeground())); g2d.drawImage( foreground.getImage(), 0, Config.getBoardDimension().getHeight() - foreground.getIconHeight(), Config.getBoardDimension().getLength(), foreground.getIconHeight(), this); } }
/** Toggle the menu */ public void toggleMenu() { if (c.getLifes() > 0) { c.setPause(!c.isPaused()); showMenu = !showMenu; } }
private void paintScore(Graphics2D g2d) { Font font = new Font("sans", Font.PLAIN, 36); g2d.setColor(Color.green); g2d.setFont(font); g2d.drawString((int) c.getPoints() + "", 10, 50); }
@Override public void dispose() { closeNetworking(); super.dispose(); }
public void paintComponent(Graphics g) { super.paintComponent(g); _tG.readTile((char) _game.topTile().get(new Point(3, 3))); _rotateNum = (char) (_game.topTile().get(new Point(3, 4))) - '0'; _emptySlot = _game.get_Slot(); for (int i = 0; i < 10000; i++) { g.setColor(Color.black); g.fillRect( (i % 100) * squareSize + (i % 100 + 1) * 1, (i / 100) * squareSize + (i / 100 + 1), squareSize, squareSize); } for (Point p : _emptySlot) { g.setColor(Color.green); g.fillRect(p.x * (squareSize + 1) + 1, p.y * (squareSize + 1) + 1, 80, 80); } _tilePiece = cutImage(TileGeneratorView.k, TileGeneratorView.j); for (int i = 0; i < _rotateNum; i++) { _tilePiece = rotateImage(_tilePiece, 90); } tilePiece = _tilePiece; g.drawImage(tilePiece, x, y, 80, 80, this); HashMap<Point, HashMap<Point, Object>> gameBoard = _game.get_gameBoard(); for (HashMap.Entry<Point, HashMap<Point, Object>> entry : gameBoard.entrySet()) { int j = -1, k = -1; // using j,k to indicate where are the tiles locate in our picture. Point position = entry.getKey(); int xp = (int) position.getX(); int yp = (int) position.getY(); HashMap<Point, Object> tileMap = entry.getValue(); int xMeeple = 10, yMeeple = 10; for (HashMap.Entry<Point, Object> entry1 : tileMap.entrySet()) { Point meeplePosition = entry1.getKey(); char parts = (char) entry1.getValue(); // System.out.println(parts); if (parts >= 'a' && parts <= 'z') { xMeeple = (int) meeplePosition.getX(); yMeeple = (int) meeplePosition.getY(); } } char tile = (char) entry.getValue().get(new Point(3, 3)); switch (tile) { case 'A': j = 3; k = 4; break; case 'B': j = 3; k = 3; break; case 'C': j = 1; k = 2; break; case 'D': j = 2; k = 0; break; case 'E': j = 0; k = 0; break; case 'F': j = 1; k = 1; break; case 'G': j = 1; k = 0; break; case 'H': j = 0; k = 2; break; case 'I': j = 0; k = 1; break; case 'J': j = 2; k = 2; break; case 'K': j = 2; k = 1; break; case 'L': j = 3; k = 2; break; case 'M': j = 0; k = 4; break; case 'N': j = 0; k = 3; break; case 'O': j = 2; k = 4; break; case 'P': j = 2; k = 3; break; case 'Q': j = 1; k = 4; break; case 'R': j = 1; k = 3; break; case 'S': j = 3; k = 1; break; case 'T': j = 3; k = 0; break; case 'U': j = 4; k = 0; break; case 'V': j = 4; k = 1; break; case 'W': j = 4; k = 2; break; case 'X': j = 4; k = 3; break; } Image tempTile; tempTile = cutImage(k, j); int rotateNumber = 0; char rotateNum = (char) entry.getValue().get(new Point(3, 4)); switch (rotateNum) { case '0': rotateNumber = 0; break; case '1': rotateNumber = 1; break; case '2': rotateNumber = 2; break; case '3': rotateNumber = 3; break; } tempTile = rotateImage(tempTile, rotateNumber * 90); g.drawImage(tempTile, xp * (squareSize + 1) + 1, yp * (squareSize + 1) + 1, 80, 80, this); Image meeple; meeple = cutMeeple(); meeple = PutMeepleView.makeColorTransparent((BufferedImage) meeple, Color.white); // if((char)tileMap.get(new Point(3,5)) = 'a'){ // System.out.println(xMeeple); // System.out.println(yMeeple); if (xMeeple < 10 && yMeeple < 10) { g.drawImage( meeple, xp * (squareSize + 1) + 1 + 27 * xMeeple, yp * (squareSize + 1) + 1 + 27 * yMeeple, 27, 27, this); } // } } }
public void redrawComponents(Game game, Board board) { jButton4.setEnabled(game.canUndoMove()); }
/** paintComponent Draws the level of the game */ public void paintComponent(Graphics g) { super.paintComponent(g); game.getLevel().draw(g); }
/** * The View(Board board, JTextArea textBox) constructor takes a board and textBox as parameters * and sets these to the corresponding fields in the View object. String text is initially set to * "". Then, actionlisteners are added to the View. * * @param board * @param textBox */ public View(Board board, JTextArea textBox, Game game) { this.board = board; this.game = game; this.textBox = textBox; text = ""; /* * This MouseListener governs what happens in the event that a mouse is pressed. * The mouseListener cycles through all the squares on the board, and for all the * squares that have pieces, it checks if the place where the person clicked * is within the piece. If it is, then that piece is set to selected. */ /* * The mousepressed listener for Player vs. Player mode */ if (game.getMode() == 0) { addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent event) { mousePoint = event .getPoint(); // sets mousepoint to the current point for when the mouse is // dragged // this for loop cycles through all squares on the board if (!View.this.game.getVictory()) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (View.this.board.hasPiece(i, j)) { // checks if board square has a piece if (View.this .board .getSquare(i, j) .contains( event .getPoint())) { // checks if piece contains the place mouse was // clicked if (View.this.board.getSquare(i, j).getColor() == View.this.game.getTurn()) { // check if it is the right turn View.this.board.getSquare(i, j).setSelected(true); // set selected to true View.this.text += View.this.board.getSquare(i, j).getType() + " selected! \n"; // add to console View.this.textBox.setText(View.this.text); } } } } } } } }); } /* * The mousepressed listener for modes Captain Random and Minimax */ if (game.getMode() != 0) { addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent event) { mousePoint = event .getPoint(); // sets mousepoint to the current point for when the mouse is // dragged // this for loop cycles through all squares on the board if (!View.this.game.getVictory()) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (View.this.board.hasPiece(i, j)) { // checks if board square has a piece if (View.this .board .getSquare(i, j) .contains( event .getPoint())) { // checks if piece contains the place mouse was // clicked if (View.this.game.getTurn() == false && View.this.board.getSquare(i, j).getColor() == false) { // check if it is the right turn View.this.board.getSquare(i, j).setSelected(true); // set selected to true View.this.text += View.this.board.getSquare(i, j).getType() + " selected! \n"; // add to console View.this.textBox.setText(View.this.text); } } } } } } } }); } /* * this actionListener governs what happens when the mouse is dragged */ addMouseMotionListener( new MouseMotionAdapter() { public void mouseDragged(MouseEvent event) { Point lastMousePoint = mousePoint; // mousePoint used to keep track of where the piece should be drawn mousePoint = event.getPoint(); for (int i = 0; i < 8; i++) { // nested for loops iterate through the whole board for (int j = 0; j < 8; j++) { if (View.this.board.hasPiece(i, j)) { if (View.this.board.getSquare(i, j).isSelected()) { double dx = mousePoint.getX() - lastMousePoint.getX(); // calculate how much to change piece position double dy = mousePoint.getY() - lastMousePoint.getY(); View.this .board .getSquare(i, j) .translate((int) dx, (int) dy); // translate piece } } } } repaint(); // repaints the View everytime mouse is dragged } }); /* * This actionListener governs what happens when the mouse is released */ addMouseListener( new MouseAdapter() { public void mouseReleased(MouseEvent e) { boolean pieceSelected = false; // keeps track of whether or not a piece was selected Piece selectedPiece = null; // keeps track of the selected piece Piece eatenPiece = null; // keeps track of piece on square another piece was moved to int oldX = 0; // keeps track of prior position if piece int oldY = 0; // for loop cyles through board for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (View.this.board.hasPiece(i, j)) { // checks if board square has a piece if (View.this .board .getSquare(i, j) .isSelected()) { // then checks if the piece was selected pieceSelected = true; selectedPiece = View.this.board.getSquare(i, j); // records data about piece oldX = i; oldY = j; } } } } if (pieceSelected) { if (selectedPiece.checkLegalMove( e.getPoint(), View.this.board)) { // check legal moves int newX = ((int) e.getPoint().getX()) / 62; // record new point int newY = ((int) e.getPoint().getY()) / 62; /* * Make the move */ // check for castling here if (selectedPiece.getType().equals("King") && newX - oldX == 2) { if (selectedPiece.getColor() == false) { // case that piece is white View.this.board.clearSquare(oldX, oldY); View.this.board.setSquare(newX, newY, selectedPiece); selectedPiece.setLocation(newX, newY); Piece rook = View.this.board.getSquare(7, 7); View.this.board.setSquare(5, 7, rook); rook.setLocation(5, 7); View.this.board.clearSquare(7, 7); View.this.text += "White castles. \n"; } else { // case that piece is black View.this.board.clearSquare(oldX, oldY); View.this.board.setSquare(newX, newY, selectedPiece); selectedPiece.setLocation(newX, newY); Piece rook = View.this.board.getSquare(7, 0); View.this.board.setSquare(5, 0, rook); rook.setLocation(5, 0); View.this.board.clearSquare(7, 0); View.this.text += "White castles. \n"; } } else { if (View.this.board.hasPiece(newX, newY)) { eatenPiece = View.this.board.getSquare(newX, newY); } View.this.board.clearSquare(oldX, oldY); View.this.board.setSquare(newX, newY, selectedPiece); // snap to square if (selectedPiece.setLocation(newX, newY)) System.out.println("location set"); // debug line if ((eatenPiece != null)) { View.this.text += selectedPiece.getType() + " eats " + eatenPiece.getType() + "\n"; View.this.textBox.setText(View.this.text); } View.this.text += selectedPiece.getType() + " was moved to: " + View.this.columns[newX] + (newY + 1) + "\n"; System.out.println("Snapped to square"); } // repaint(); /*Note: newY+1 is used to * read in the console as if rows and cols start *from 1 instead of 0, as they do in the program* */ View.this.textBox.setText(View.this.text); // record to console View.this.game.checkVictory(View.this.board); if (View.this.game.getVictory()) { if (View.this.game.getTurn() == false) { View.this.text += "White has taken black's king and won the game!"; } else { View.this.text += "Black has taken white's king and won the game!"; } View.this.textBox.setText(View.this.text); } else { View.this.game.changeTurn(); if (View.this.game.getMode() != 0) { // case that the game is playing with AI /* * This Thread is what runs in teh background and computes the AI's best * move and makes that move. It also updates the textArea with the appropriate text */ Thread t = new Thread( new Runnable() { public void run() { View.this.text += View.this.game.getAI().makeMove(View.this.board); View.this.textBox.setText(View.this.text); View.this.game.checkVictory(View.this.board); if (View.this.game.getVictory()) { if (View.this.game.getTurn() == false) { View.this.text += "White has taken black's king and won the game!"; } else { View.this.text += "Black has taken white's king and won the game!"; } View.this.textBox.setText(View.this.text); } else { View.this.game.changeTurn(); } repaint(); // repaints board after AI makes move } }); // Have AI make move if game is in AI mode t.start(); // begins thread } } } else { // case that move was illegal // snap back to original square selectedPiece.setLocation(oldX, oldY); View.this.text += "Illegal move! \n"; View.this.textBox.setText(View.this.text); } selectedPiece.setSelected(false); } repaint(); } }); }
@Override public void endTheGame(String message) { super.endTheGame(message); closeNetworking(); addMenu(new EndGameMenu()); }