📄 game.java
字号:
import java.awt.*;import java.awt.event.*;class Game extends Panel { // data private String _name; private Board _board; private StatusBar _status; private boolean _finished; private Players _players; private FieldFactory _factory; private int _width; private int _height; private int _cellsize; private int _winningSequence; // construction public Game() { _name = "TicTacToe"; setLayout(new BorderLayout()); // game parameters _width = 3; _height = 3; _cellsize = 100; _winningSequence = 3; // initialize players _players = new Players(); _players.add(new Player("One", new CrossToken())); _players.add(new Player("Two", new OToken())); _players.select("One"); // initialize board _factory = new FieldFactory(this, new Dimension(_cellsize,_cellsize)); _board = new Board(_width, _height, _factory); add(_board, "Center"); // initialize status bar _status = new StatusBar(this); add(_status, "South"); _players.addObserver(_status); // set size of the panel: board + statusbar setSize(new Dimension((int)(1.05 * _width * _cellsize), (int)(1.05 * _height * _cellsize) + _status.getSize().height)); // start a new game newGame(); } // the name of the game public String getName() { return _name; } // restarting the game public void newGame() { _board.clear(); _players.select("One"); setNotFinished(); } // next player public void nextPlayer() { _players.next(); } public Player getCurrentPlayer() { return _players.getCurrent(); } // finished public boolean getFinished() { return _finished; } public void setFinished() { _finished = true; _status.update(null,null); } public void setNotFinished() { _finished = false; _status.update(null,null); } // length of winning sequence public int getWinningSequence() { return _winningSequence; } public void paint(Graphics g) { Dimension d = getSize(); Color bg = getBackground(); g.setColor(bg); g.draw3DRect(0, 0, d.width - 1, d.height - 1, true); g.draw3DRect(3, 3, d.width - 7, d.height - 7, false); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -