Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[BOARD VIEW]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; /*board */ public class BoardView extends Canvas { /** *BoardView */ private CheckersBoard board; /**

[BOARD VIEW]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*;

/*board */ public class BoardView extends Canvas { /** *BoardView */ private CheckersBoard board;

/** * upper left */ int startX; int startY;

/** * width */ int cellWidth;

/** * select

*/ List selected;

/** * Computer */ Computer computer; /** * */ private static final int SIZE = 0;

/** * */ private Frame parent;

/** * */ private MouseHandler handler;

/** * Constructor */ public BoardView (Frame parentComponent, CheckersBoard b) { selected = new List (); board = b; parent = parentComponent; computer = new Computer (b); handler = new MouseHandler (this, parent); addMouseListener (handler); }

/** * */ public CheckersBoard getBoard () { return board; }

/** * new game */ public void newGame () { board.clearBoard (); selected.clear (); repaint (); handler.reset (); computer.play (); ChangeTitle (); }

/** * current player

*/ public void ChangeTitle () { if (board.getCurrentPlayer () == CheckersBoard.WHITE) parent.setTitle ("Checkers - Brancas"); else parent.setTitle ("Checkers - Pretas"); }

/** * save board */ public void saveBoard (String fileName) { try { FileOutputStream ostream = new FileOutputStream (fileName); ObjectOutputStream p = new ObjectOutputStream(ostream); p.writeObject(board); p.flush(); ostream.close(); } catch (IOException e) { e.printStackTrace (); System.exit (1); } }

/** * load board */ public void loadBoard (String fileName) { try { FileInputStream istream = new FileInputStream(fileName); ObjectInputStream p = new ObjectInputStream(istream); board = (CheckersBoard) p.readObject(); istream.close(); repaint (); computer.setBoard (board); ChangeTitle (); } catch (Exception e) { e.printStackTrace (); System.exit (1); } } /** *. */ public void paint (Graphics g) { Dimension d = getSize (); int marginX; int marginY; int incValue;

Image image = createImage (d.width, d.height); Graphics gBuff = image.getGraphics ();

// Limpa o buffer gBuff.setColor (Color.lightGray); gBuff.fillRect (0, 0, d.width, d.height); gBuff.setColor (Color.black);

// // quadrado if (d.width < d.height) { marginX = 0; marginY = (d.height - d.width) / 2; incValue = d.width / 8; } else { marginX = (d.width - d.height) / 2; marginY = 0; incValue = d.height / 8; }

startX = marginX; startY = marginY; cellWidth = incValue; drawBoard (gBuff, marginX, marginY, incValue); drawPieces (gBuff, marginX, marginY, incValue);

g.drawImage (image, 0, 0, this); }

/** * */ private void drawBoard (Graphics g, int marginX, int marginY, int incValue) { int pos; for (int y = 0; y < 8; y++) for (int x = 0; x < 8; x++) { if ((x + y) % 2 == 0) g.setColor (Color.white); else { pos = y * 4 + (x + ((y % 2 == 0) ? - 1 : 0)) / 2; if (selected.has (new Integer (pos))) g.setColor (Color.green); else g.setColor (Color.black); }

g.fillRect (marginX + x * incValue, marginY + y * incValue, incValue - 1, incValue - 1); } }

/** * */ private static final int KING_SIZE = 3; /** */ private void drawPieces (Graphics g, int marginX, int marginY, int incValue) { int x, y; for (int i = 0; i < 32; i++) try { if (board.getPiece (i) != CheckersBoard.EMPTY) { if (board.getPiece (i) == CheckersBoard.BLACK || board.getPiece (i) == CheckersBoard.BLACK_KING) g.setColor (Color.red); else g.setColor (Color.white);

y = i / 4; x = (i % 4) * 2 + (y % 2 == 0 ? 1 : 0); g.fillOval (SIZE + marginX + x * incValue, SIZE + marginY + y * incValue, incValue - 1 - 2 * SIZE, incValue - 1 - 2 * SIZE);

if (board.getPiece (i) == CheckersBoard.WHITE_KING) { g.setColor (Color.black); g.drawOval (KING_SIZE + marginX + x * incValue, KING_SIZE + marginY + y * incValue, incValue - 1 - 2 * KING_SIZE, incValue - 1 - 2 * KING_SIZE); } else if (board.getPiece (i) == CheckersBoard.BLACK_KING) { g.setColor (Color.white); g.drawOval (KING_SIZE + marginX + x * incValue, KING_SIZE + marginY + y * incValue, incValue - 1 - 2 * KING_SIZE, incValue - 1 - 2 * KING_SIZE); } } } catch (BadCoord bad) { bad.printStackTrace (); System.exit (1); } } }

/** * */ class MouseHandler extends MouseAdapter { /** * */ private BoardView view;

/** */ private Frame parent;

Stack boards; public MouseHandler (BoardView boardView, Frame parentComponent) { view = boardView; parent = parentComponent; boards = new Stack (); }

public void mouseClicked (MouseEvent e) { int pos; MessageBox msg;

pos = getPiecePos (e.getX (), e.getY ()); if (pos != -1) try { CheckersBoard board = view.getBoard ();

int piece = board.getPiece (pos); if (piece != CheckersBoard.EMPTY && (((piece == CheckersBoard.WHITE || piece == CheckersBoard.WHITE_KING) && board.getCurrentPlayer () == CheckersBoard.WHITE) || ((piece == CheckersBoard.BLACK || piece == CheckersBoard.BLACK_KING) && board.getCurrentPlayer () == CheckersBoard.BLACK))) { if (view.selected.isEmpty ()) // view.selected.push_back (new Integer (pos)); else { int temp = ((Integer) view.selected.peek_tail ()).intValue ();

if (temp == pos) view.selected.pop_back (); else { msg = new MessageBox (parent, "Error"); msg.setVisible (true); } } view.repaint (); return; } else { boolean good = false; CheckersBoard tempBoard; if (!view.selected.isEmpty ()) { if (boards.empty ()) { tempBoard = (CheckersBoard) board.clone (); boards.push (tempBoard); } else tempBoard = (CheckersBoard) boards.peek ();

int from = ((Integer) view.selected.peek_tail ()).intValue (); if (tempBoard.isValidMove (from, pos)) { tempBoard = (CheckersBoard) tempBoard.clone ();

boolean isAttacking = tempBoard.mustAttack (); tempBoard.move (from, pos); if (isAttacking && tempBoard.mayAttack (pos)) { view.selected.push_back (new Integer (pos)); boards.push (tempBoard); view.repaint (); } else { view.selected.push_back (new Integer (pos)); makeMoves (view.selected, board); boards = new Stack (); } good = true; } else if (from == pos) { view.selected.pop_back (); boards.pop (); view.repaint (); good = true; } } if (!good) { msg = new MessageBox (parent, "Error"); msg.setVisible (true); } } } catch (BadCoord bad) { bad.printStackTrace (); System.exit (1); } catch (BadMoveException bad) { bad.printStackTrace (); System.exit (1); } }

/** * */ public void reset () { boards = new Stack (); }

/** * */ private void makeMoves (List moves, CheckersBoard board) throws BadMoveException { List moveList = new List (); int from, to = 0;

from = ((Integer) moves.pop_front ()).intValue (); while (!moves.isEmpty ()) { to = ((Integer) moves.pop_front ()).intValue (); moveList.push_back (new Move (from, to)); from = to; }

board.move (moveList); view.repaint (1); view.selected.clear (); reset ();

if (!gameEnded ()) { view.ChangeTitle (); view.computer.play (); view.repaint ();

if (!gameEnded ()) view.ChangeTitle (); } } private int getPiecePos (int currentX, int currentY) { for (int i = 0; i < 32; i++) { int x, y;

y = i / 4; x = (i % 4) * 2 + (y % 2 == 0 ? 1 : 0); if (view.startX + x * view.cellWidth < currentX && currentX < view.startX + (x + 1) * view.cellWidth && view.startY + y * view.cellWidth < currentY && currentY < view.startY + (y + 1) * view.cellWidth) return i; }

return -1; }

/** * */ private boolean gameEnded () { MessageBox msg; CheckersBoard board = view.getBoard (); boolean result;

int white = board.getWhitePieces (); int black = board.getBlackPieces (); if (board.hasEnded ()) { if (board.winner () == CheckersBoard.BLACK) msg = new MessageBox (parent); else msg = new MessageBox (parent); msg.setVisible (true); result = true; } else result = false;

return result; } }

//////part of checkers program using tree- what is it doing- convert to c++ codeblock please

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions