📄 rulesframe.java
字号:
import java.awt.*;import java.awt.event.*;/** * Sets up a new window that contains the rules of the various games. */public class RulesFrame extends Frame implements ActionListener { private Games _games; private TextArea _rules; // menus MenuBar _menuBar; private Menu _fileMenu; private MenuItem _exitItem; private Menu _gamesMenu; private MenuItem _ticItem; private MenuItem _fiarItem; private MenuItem _goItem; private MenuItem _othelloItem; public RulesFrame(Games games) { _games = games; // Frame setLayout(new BorderLayout()); setTitle("Rules"); setSize(400,400); setupMenuBar(); addWindowListener(new WindowDisposer(this)); // textArea _rules = new TextArea("Select a game from the Games menu.", 20, 30, 1); _rules.setBackground(new Color(160,160,130)); _rules.setEditable(false); add(_rules, "Center"); if(_games.getCurrent() != null) showRules(_games.getCurrent().getName()); setVisible(true); } public void setupMenuBar() { _menuBar = new MenuBar(); setMenuBar(_menuBar); // File menu _fileMenu = new Menu("File"); _menuBar.add(_fileMenu); _exitItem = new MenuItem("Close"); _fileMenu.add(_exitItem); _fileMenu.addActionListener(this); // Games menu _gamesMenu = new Menu("Games"); _menuBar.add(_gamesMenu); _gamesMenu.addActionListener(this); _ticItem = new MenuItem("TicTacToe"); _gamesMenu.add(_ticItem); _ticItem.addActionListener(this); _fiarItem = new MenuItem("Four-in-a-Row"); _gamesMenu.add(_fiarItem); _fiarItem.addActionListener(this); _goItem = new MenuItem("Go-Bang"); _gamesMenu.add(_goItem); _goItem.addActionListener(this); _othelloItem = new MenuItem("Othello"); _gamesMenu.add(_othelloItem); _othelloItem.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand() == "Close") dispose(); else showRules(e.getActionCommand()); } public void showRules(String name) { if(name == "TicTacToe") ticRules(); else if(name == "Four-in-a-Row") fiarRules(); else if(name == "Go-Bang") gobangRules(); else if(name == "Othello") othelloRules(); } public void ticRules() { setTitle("Rules for Tic-Tac-Toe"); _rules.setText(tictext); } public void fiarRules() { setTitle("Rules for Four-in-a-Row"); _rules.setText(fiartext); } public void gobangRules() { setTitle("Rules for Go-Bang"); _rules.setText(gobangtext); } public void othelloRules() { setTitle("Rules for Othello"); _rules.setText(othellotext); } String tictext = "The object of the game is to get three tokens in a row, horizontally, vertically or diagonally. \n\nYou start the game by clicking on the field you want the first token in. Then it is the other players turn to place one of his tokens in a field. \n\nThe game is over when a player has three tokens in a row, and this player has then won the game, or when all fields are occupied by a token."; String fiartext = "The object of the game is to get four tokens in a row, horizontally, vertically or diagonally. \n\nIt is only possible to place tokens in the buttom row the first time. After that a player can place tokens on top of others, as long as they are directly below. \n\nYou start the game by clicking on the field you want the first token in. Then it is the other players turn to place one of his tokens in a field. \n\nThe game is over when a player has four tokens in a row, and this player has then won the game, or when all fields are occupied by a token."; String gobangtext = "The object of the game is to get five tokens in a row, horizontally, vertically or diagonally. \n\nYou start the game by clicking on the field you want the first token in. Then it is the other players turn to place one of his or hers tokens in a field. \n\nThe game is over when a player has five tokens in a row, and this player has then won the game."; String othellotext = "First, some terminology \n\nA legal move consists of outflanking your opponent's token(s), then flipping the outflanked token(s) to your color. All moves must place one of your tokens so that one or more of your opponents tokens are flanked by your tokens. \n\nTo outflank means to place a token on the board so that your opponent's row (or rows) of token(s) is bordered at each end by a token of your color. \n\nA row is defined as one or more tokens in a continuous straight line. This may be straight lines or diagonal lines. \n\n\nNow, the official rules \n\nBlack always moves first. \n\nIf on your turn you cannot outflank and flip at least one opposing tokens, your move is forfeited and your opponent moves again. However, if a move is available to you, you may not forfeit your turn. \n\nA token may outflank any number of tokens in one or more rows in any number of directions at the same time - horizontally, vertically or diagonally. \n\nYou may not skip over your own color tokens to outflank an opposing disc. \n\nToken(s) may only be outflanked as a direct result of a move and must fall in the direct line of the token placed down. If your opponents tokens are outflanked as a result of flipping over tokens after a move, they are not flipped. \n\nAll tokens outflanked in any one move must be flipped, even if it is to the player's advantage not to flip them all. \n\nWhen it is no longer possible for either player to move, the game is over. Tokens are counted and the player with the majority of his or her color tokens on the board is the winner."; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -