java/ net beans
please write the function of each line in this
coding:
java/ net beans
please write the function of each line in this
coding:
P6: Interactive Game • Implementation of a basic puzzle/rpg (Role Playing Game) /board type game with user interactivity. • Use of at least a basic level of heuristic in the difficulty of the game • Multiple outcome scenarios, with user choices being crucial at least at 3 points. (Tic Tac Toe has a minimum of 3 crucial decisions.) • Ability to save/load game states. • Challenge: rudimentary graphics display, using either ASCII character set or other such methods.
1 þackage javaapplication43; 2. 3 Ç import java.awt.Canvas; 4 import java.awt.Color; 5 import java.awt.Component; 6 import java.awt. Dimension; 7 import java.awt. Font; 8 import java.awt.Graphics2D; 9 import java.awt.event.KeyEvent; 10 import java.awt.event.MouseEvent; 11 import java.awt.event.WindowAdapter; 12 import java.awt.event.WindowEvent; 13 import java.awt.image. Bufferstrategy; 14 15 import javax.swing.JFrame; 16 import javax.swing.JPanel; 17 18 public class TicTacToe extends Canvas { 20 21 private static final long serialversionUID = 3086638359652751354L; 22 23 private final int SCREEN_WIDTH = 800; 24 private final int SCREEN_HEIGHT = 600; 25 26 private final Bufferstrategy strategy; 27 28 private int mousePressedx = -1; 29 private int mouse Pressedy = -1; 30 private boolean enterPressed = false; 31 32 private int leftEdge = 0;
private int topEdge = 0; 33 34 35 36 private int cell Centre = 0; private int cellvCentre = 0; 37 38 private char currentMove = 'X'; private char gameWinner = '0'; 39 40 41 42 43 private final char[] [] gameBoard = new char[3] [3]; private int moves = 0; 44 45 46 47 48 public TicTacToe () throws InterruptedException { JFrame container = new JFrame ("Tic Tac Toe"); JPanel panel = (JPanel) container.getContentPane (); panel.setPreferredSize (new Dimension (SCREEN_WIDTH, SCREEN_HEIGHT)); panel.setLayout (null); setBounds (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); Component add = panel.add(this); 49 50 SA 52 53 leftEdge = SCREEN_WIDTH / 3; topEdge = SCREEN_HEIGHT / 3; 54 55 56 57 cell Centre = leftEdge / 2; cell centre = topEdge / 2; 58 59 60 setIgnoreRepaint(true); container.pack(); container.setResizable (false); container.setVisible (true); 61 62 63 64 container.addwindowListener(new WindowAdapter() { 로
65 67 @Override public void windowClosing (WindowEvent e) { System.exit(0); } 67 68 69 }); 70 71 72 73 addKeyListener(new KeyboardListener()); addMouseListener (new MouseListener()); request Focus(); createBufferStrategy (2); strategy = getBuffer Strategy (); 74 ごり 75 76 } 77 78 로 79 80 81 private void newGame () { System.out.println("New game starting..."); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { gameBoard[j] = '0'; } } = 82 83 84 85 86 moves = 9; gameWinner = '0'; 87 88 } 89 90 91 로 private void nextMove () { if (currentMove == 'X') currentMove = '0'; else 92 93 94 currentMove = "X'; 95 moves--; 96 }
97 98 요 100 public void gameLoop() { long delta = 0; long lastLoopTime = System.currentTimeMillis(); newGame (); 101 102 103 104 while (true) { delta = System.currentTimeMillis() lastLoopTime; lastLoopTime = System.currentTimeMillis(); 105 106 107 Graphics2D g = (Graphics2D) strategy.getDrawGraphics(); 108 109 110 111 112 SA events (delta); draw(g); update (g); try { Thread. sleep (10); } catch (InterruptedException e) { System.out.println(e.getMessage()); } 114 115 116 117 } 118 119 120 121 122 123 124 private void events (long delta) { if ((mouse Pressedx >= 0 | mouse Pressedy >= 0 && gameWinner == '0') { int xIndex = (int) Math.floor(mousePressedx / leftEdge); int yIndex = (int) Math.floor(mouse Pressedy / topEdge); 125 126 if (gameBoard [yIndex] [xIndex] == '0') { gameBoard [yIndex] [xIndex] = currentMove; nextMove (); 127 } 128 129
if (gameBoard[0][0] == gameBoard[1][1] && gameBoard[0][0] == gameBoard[2][2]) { if (gameBoard[0][0] == 'X') gameWinner = 'X'; else if (gameBoard[0][0] == '0') gameWinner = '0'; } else if (gameBoard[0] [2] == gameBoard[1][1] && gameBoard[0][2] == gameBoard[2][0]) { if (gameBoard[0][2] == 'X') gameWinner = ''; else if (gameBoard[0][2] == '0') gameWinner = '0'; 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 } else { for (int i = 0; i < 3; i++) { if (gameBoard [0] == gameBoard[1] && gameBoard [0] == gameBoard [2]) { if (gameBoard [0] == 'X') gameWinner = ''; else if (gameBoard [0] == '0') game Winner = '0'; } else if (gameBoard[0] == gameBoard[1] && gameBoard[0] == game Board[2] [i]) { if (gameBoard[0][i] == 'X') gameWinner = 'X'; else if (gameBoard[0][i] == '0') gameWinner = '0';
} 162 L 163 164 A 165 166 private void draw (Graphics2D g) { g.setColor (new Color (245, 245, 245)); g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); 167 168 169 170 g.setColor (new Color (199, 199, 199)); g.drawLine (0, topEdge, SCREEN_WIDTH, topEdge); g.drawLine(0, topEdge * 2, SCREEN_WIDTH, topEdge * 2); g.drawLine (leftEdge, 0, leftEdge, SCREEN_HEIGHT); g.drawLine (leftEdge * 2, 0, leftEdge * 2, SCREEN_HEIGHT); 171 172 173 174 g. setFont (new Font ("Verdana", 0, 60)); 175 176 177 178 179 for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (gameBoard[i][j] != '0') { int xPos = j * leftEdge + cell-centre; int ypos = i * topEdge cellvcentre; 180 181 182 183 184 185 if (game Board[i][j] == 'X') { int strWidthoffset = g.getFontMetrics ().stringWidth ("x") / 2; g.setColor (new Color (198, 113, 113)); g.drawstring ("X", XPOS - strWidthoffset, ypos + 30); } else { int strWidthoffset = g.getFontMetrics ().stringWidth ("0") / 2; g.setColor (new Color (113, 198, 113)); g.drawstring ("O", xPos - strWidthoffset, yPos + 30); 186 187 188 189 190 191 192 193
194 195 g. setFont (new Font ("Verdana", 0, 24)); 196 197 198 199 200 201 202 203 204 205 206 207 if (gameWinner != '0') { g.setColor (new Color (245, 245, 245)); g.fillRect(0, SCREEN_HEIGHT - 44, SCREEN_WIDTH, 44); g.setColor (new Color (255, 153, 18)); String output = "Player " + gameWinner + " wins! (Press enter to play again)"; int fontWidth = g.get FontMetrics().stringWidth (output); g.drawstring (output, SCREEN_WIDTH/ 2 - fontWidth / 2, SCREEN_HEIGHT - 10); if (enter Pressed) newGame (); } else if (moves == 0) { g.setColor (new Color (245, 245, 245)); g.fillRect(0, SCREEN_HEIGHT - 44, SCREEN_WIDTH, 44); g.setColor (new Color (255, 153, 18)); String output = "Tie game! (Press enter to play again)"; int fontWidth = g.getFontMetrics ().stringWidth(output); g.drawstring (output, SCREEN_WIDTH / 2 - fontWidth / 2, SCREEN_HEIGHT - 10); if (enter Pressed) newGame (); 208 209 210 211 212 213 214 215 216 217 218 E 219 private void update (Graphics2D g) { g.dispose(); strategy.show(); } 220 221 222 223 224 private class KeyboardListener implements java.awt.event.KeyListener { @Override public void keyPressed (KeyEvent e) {
java/ net beans please write the function of each line in this coding: java/ net beans please write the function of each
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am