// draw N-by-N percolation system public static void draw(Percolation perc, int N) { StdDraw.clear(); StdDraw.setPenColor(StdDraw.BLACK); StdDraw.setXscale(0, N); StdDraw.setYscale(0, N); StdDraw.filledSquare(N / 2.0, N / 2.0, N / 2.0); // draw N-by-N grid int opened = 0; for (int row = 1; row <= N; row++) { for (int col = 1; col <= N; col++) { if (perc.isFull(row, col)) StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE); else if (perc.isOpen(row, col)) StdDraw.setPenColor(StdDraw.WHITE); else StdDraw.setPenColor(StdDraw.BLACK); StdDraw.filledSquare(col - 0.5, N - row + 0.5, 0.45); if (perc.isOpen(row, col)) opened++; } } // write status text StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 12)); StdDraw.setPenColor(StdDraw.BLACK); StdDraw.text(.25 * N, -N * .025, opened + " open sites"); if (perc.percolates()) StdDraw.text(.75 * N, -N * .025, "percolates"); else StdDraw.text(.75 * N, -N * .025, "does not percolate"); }
/** Plays the game. */ public void run() { StdDraw.setXscale(0, 10); StdDraw.setYscale(0, 10); StdDraw.text(5.0, 8.0, "Focus"); StdDraw.text(5.0, 6.0, "Get your pieces on top of the piles."); StdDraw.text(5.0, 4.0, "The first player unable to move loses."); StdDraw.text(5.0, 2.0, "Click to continue."); StdDraw.show(0); waitForClick(); while (!model.isGameOver()) { draw(getCurrentPlayerName() + ", click on one of your piles or your reserves.", null); Location source; do { source = waitForClick(); } while (source == null || !model.isLegalSource(source)); draw(getCurrentPlayerName() + ", click on destination square.", source); Location destination; do { destination = waitForClick(); } while (destination == null || !model.isLegalMove(source, destination)); model.move(source, destination); model.toggleColorToPlay(); } model.toggleColorToPlay(); draw(getCurrentPlayerName() + " wins.", null); }
// 游戏结束时的显示信息 public void endInfo() { StdDraw.setPenColor(StdDraw.MAGENTA); StdDraw.setFont(font1); if (getShit) StdDraw.text(100, 182, "你踩到了猫屎!"); else if (cat.escaped()) StdDraw.text(100, 182, "你竟然让猫跑了!"); else StdDraw.text(100, 182, "你用了 " + count + " 步抓住了猫!"); StdDraw.show(); }
private void drawNode(TreePosition p, Node x) { StdDraw.setPenColor(Color.GREEN); StdDraw.filledCircle(p.x, p.y, 0.3); StdDraw.setPenColor(Color.RED); StdDraw.text(p.x, p.y, x.key.toString()); StdDraw.setPenColor(Color.BLACK); }
/** Draws the state of board, including instructions or a game over message. */ public static void draw(char[][] board, char player) { StdDraw.clear(); StdDraw.line(0.5, -0.5, 0.5, 2.5); StdDraw.line(1.5, -0.5, 1.5, 2.5); StdDraw.line(-0.5, 0.5, 2.5, 0.5); StdDraw.line(-0.5, 1.5, 2.5, 1.5); for (int x = 0; x < board.length; x++) { for (int y = 0; y < board[x].length; y++) { StdDraw.text(x, y, board[x][y] + ""); } } if (winner(board) != '-') { StdDraw.text(1.0, 0.75, winner(board) + " wins!"); } else if (isFull(board)) { StdDraw.text(1.0, 0.75, "Tie game."); } else { StdDraw.text(1.0, 0.75, player + " to play. Click in a square. Three in a row wins."); } StdDraw.show(0); }
/** * Draws the state of the model, including instructions. * * @param source If non-null, drawing highlights all legal moves from source. */ public void draw(String instructions, Location source) { StdDraw.clear(); for (int r = 0; r < FocusModel.BOARD_WIDTH; r++) { for (int c = 0; c < FocusModel.BOARD_WIDTH; c++) { Location destination = new Location(r, c); drawSquare( model.getPile(destination), 1.5 + c, 9.5 - r, source != null && model.isLegalMove(source, destination)); } } StdDraw.setPenColor(LIGHT_BLUE); StdDraw.filledRectangle(2, 0.5, 2, 0.45); StdDraw.filledRectangle(8, 0.5, 2, 0.45); StdDraw.setPenColor(); StdDraw.text(2, 0.5, "Black reserves: " + model.getReserves(FocusModel.BLACK)); StdDraw.text(8, 0.5, "White reserves: " + model.getReserves(FocusModel.WHITE)); StdDraw.text(5, 1.5, instructions); StdDraw.show(0); }
// 描绘当前场景 public void show(int t) { StdDraw.clear(); StdDraw.setFont(font1); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (board[i][j] <= 0) StdDraw.setPenColor(StdDraw.LIGHT_GRAY); else StdDraw.setPenColor(StdDraw.ORANGE); StdDraw.filledCircle(getX(i, j), getY(i), 10); // 画格子 if (board[i][j] == 20) drawShit(getX(i, j), getY(i)); // 画猫屎 else if (board[i][j] > 0 && board[i][j] < 10) { // 填数字 StdDraw.setPenColor(StdDraw.RED); StdDraw.text(getX(i, j), getY(i) - 2, String.valueOf(board[i][j])); } } } drawCat(getX(cat.getPos() / 9, cat.getPos() % 9), getY(cat.getPos() / 9)); // 画猫 StdDraw.setFont(font2); StdDraw.setPenColor(StdDraw.ORANGE); StdDraw.text(175, 195, "已用:" + count + " 步"); // 右上角步数统计 StdDraw.show(t); // 延时 }
protected static void showGround() { for (int i = 0; i < myRect.size(); i++) { myRect.get(i).show(); if (i % 2 == 0) { myRect.get(i).contactBottom(); // contactBottom avec le sol } // else { // myRect.get(i).contactTop();// contactTop avec le haut // } if (myRect.get(0).getXGround() > 7000) { StdDraw.setPenColor(Color.BLACK); StdDraw.setFont(new Font("Dialog", Font.PLAIN, 20)); StdDraw.text(Constants.X_MAX / 2, Constants.Y_MAX / 2, "Player vs player"); } } }
// Draw one gem with given coordinates. void draw(double x, double y) { switch (type) { case BLUE: StdDraw.picture(x, y, "gem_blue.png"); break; case GREEN: StdDraw.picture(x, y, "gem_green.png"); break; case ORANGE: StdDraw.picture(x, y, "gem_orange.png"); break; } StdDraw.setFont(new Font("SansSerif", Font.BOLD, 14)); StdDraw.setPenColor(StdDraw.WHITE); StdDraw.text(x, y, "" + points); }
protected static void endGame() { // methode fin du jeu int winner = myTank.get(0).id; for (int i = 1; i < myTank.size(); i++) { if (myTank.get(i).life == 0) { winner = myTank.get(i).id; } else if (winner == 0) { // correction temporaire cas joueur 2 gagnant winner = winner + 2; } } // affichage du gagnant StdDraw.clear(); StdDraw.clear(Color.WHITE); StdDraw.setPenColor(Color.BLACK); StdDraw.text(5000, 6000, "Le joueur " + winner + " est vainqueur"); StdDraw.rectangle(Constants.X_MAX / 2, Constants.Y_MAX / 2, 6000, 2000); StdDraw.show(5000); }
public static void main(String[] args) { // check for optional command-line argument that turns on animation int delay = -1; if (args.length > 0) delay = Integer.parseInt(args[0]); // get dimensions int w = StdIn.readInt(); int h = StdIn.readInt(); StdDraw.setCanvasSize(w, h); StdDraw.setXscale(0, w); StdDraw.setYscale(0, h); // turn on animation mode StdDraw.show(0); // run smallest insertion heuristic Tour tour = new Tour(); while (!StdIn.isEmpty()) { double x = StdIn.readDouble(); double y = StdIn.readDouble(); Point p = new Point(x, y); tour.insertSmallest(p); if (delay > -1) { StdDraw.clear(); tour.draw(); StdDraw.text(100, 0, "" + tour.distance()); StdDraw.show(delay); } } // draw to standard draw tour.draw(); StdDraw.show(0); // print tour to standard output System.out.println("Tour distance = " + tour.distance()); tour.show(); }
public static void main(String[] args) { System.out.println("Si vous voulez jouer en graphique, tapez 1, sinon 0."); Scanner lecture = new Scanner(System.in); graphique = lecture.nextInt(); if (graphique == 0) { System.out.println("Avec combien de carrés voulez-vous jouer?"); Scanner scan = new Scanner(System.in); int nbcarres = scan.nextInt(); if (nbcarres == 4) { c = 3; l = 3; } if (nbcarres == 9) { c = 4; l = 4; } if (nbcarres == 16) { c = 5; l = 5; } if (nbcarres == 25) { c = 6; l = 6; } // Choix du type de jeu Scanner scn = new Scanner(System.in); System.out.println("Voulez-vous jouer contre un humain(0), ou un ordinateur(1)?"); typeJeu = scn.nextInt(); // Affichage du plateau de jeu TabManager tab1 = new TabManager(c, l); System.out.println(); tab1.initTab(); System.out.println(); System.out.println(); System.out.println(); while (true) { Test1.joueur = 1; System.out.println("Joueur A, horizontale(h) ou verticale(v)?"); Scanner dir = new Scanner(System.in); String str = dir.nextLine(); System.out.println("ligne: "); int coox = dir.nextInt(); System.out.println("colonne: "); int cooy = dir.nextInt(); if (str.charAt(0) == 'h') { TabManager.horizontale[coox - 1][cooy - 1] = 1; } if (str.charAt(0) == 'v') { TabManager.verticale[coox - 1][cooy - 1] = 1; } tab1.updateTab(); checkWin(TabManager.horizontale, TabManager.verticale); System.out.println("Points A: " + Test1.pointsA); System.out.println("Points B: " + Test1.pointsB); System.out.println("__________________________________________________"); System.out.println(); if (checkEnd(TabManager.horizontale, TabManager.verticale) == true) { System.out.println("La partie est finie!"); break; } if (typeJeu == 0) { Test1.joueur = 2; System.out.println("Joueur B, horizontale(h) ou verticale(v)?"); Scanner dire = new Scanner(System.in); String strg = dire.nextLine(); System.out.println("ligne: "); coox = dire.nextInt(); System.out.println("colonne: "); cooy = dire.nextInt(); if (strg.charAt(0) == 'h') { TabManager.horizontale[coox - 1][cooy - 1] = 1; } if (strg.charAt(0) == 'v') { TabManager.verticale[coox - 1][cooy - 1] = 1; } tab1.updateTab(); checkWin(TabManager.horizontale, TabManager.verticale); System.out.println("Points A: " + Test1.pointsA); System.out.println("Points B: " + Test1.pointsB); System.out.println("__________________________________________________"); if (checkEnd(TabManager.horizontale, TabManager.verticale) == true) { System.out.println("La partie est finie!"); break; } } if (typeJeu == 1) { Test1.joueur = 2; IA(); tab1.updateTab(); checkWin(TabManager.horizontale, TabManager.verticale); System.out.println("Points A: " + Test1.pointsA); System.out.println("Points B: " + Test1.pointsB); System.out.println("__________________________________________________"); if (checkEnd(TabManager.horizontale, TabManager.verticale) == true) { System.out.println("La partie est finie!"); break; } } } } if (graphique == 1) { Fenetre fen = new Fenetre(); while (Fenetre.b == false) { System.out.println(""); } TabManager tabg = new TabManager(c, l); StdDraw.setXscale(0, 40 + (Test1.c - 1) * 40); StdDraw.setYscale(0, 40 + (Test1.c - 1) * 40); fen.affichageGrille(TabManager.horizontale, TabManager.verticale); while (!(checkEnd(TabManager.horizontale, TabManager.verticale))) { Test1.joueur = 1; System.out.println("Joueur A"); System.out.println(Test1.b); while (Test1.b == false) { clicSouris(); } System.out.println("J1"); checkWin(TabManager.horizontale, TabManager.verticale); fen.affichageGrille(TabManager.horizontale, TabManager.verticale); Test1.b = false; if (Test1.typeJeu == 0) { Test1.joueur = 2; System.out.println("Joueur B"); while (Test1.b == false) { clicSouris(); } System.out.println("J2"); checkWin(TabManager.horizontale, TabManager.verticale); fen.affichageGrille(TabManager.horizontale, TabManager.verticale); Test1.b = false; } else { Test1.joueur = 2; IA(); System.out.println("TOP"); checkWin(TabManager.horizontale, TabManager.verticale); fen.affichageGrille(TabManager.horizontale, TabManager.verticale); Test1.b = false; } } StdDraw.clear(); StdDraw.text(50, 80, "La partie est finie!"); if (Test1.pointsA > Test1.pointsB) { StdDraw.text(50, 70, "Le joueur A remporte la partie!"); } if (Test1.pointsA < Test1.pointsB) { StdDraw.text(50, 70, "Le joueur B remporte la partie!"); } if (Test1.pointsA == Test1.pointsB) { StdDraw.text(50, 70, "Match Nul!"); } } }
public NFADemo(String regexp) { // use stack to keep track of the positions of '('and '|' // issue : or的优先级最低 Stack<Integer> ops = new Stack<Integer>(); re = regexp.toCharArray(); M = re.length; G = new Digraph(M + 1); // 第一次遍历,确定层次,仅为作图用 int[] levels = new int[M]; // levels仅在 * 和 | 处非零 for (int i = 0; i < M; i++) { int lp = i; if (re[i] == '(' || re[i] == '|') ops.push(i); else if (re[i] == ')') { int or = ops.pop(); if (re[or] == '|') { lp = ops.pop(); levels[or] = maxOfArr(levels, lp, i) + 1; } else lp = or; } if (i < M - 1 && re[i + 1] == '*') levels[i + 1] = maxOfArr(levels, lp, i + 1) + 1; } int maxLevel = maxOfArr(levels, 0, M); StdDraw.setCanvasSize(800, 800 * 2 * maxLevel / M); StdDraw.setXscale(0, M); StdDraw.setYscale(-maxLevel, maxLevel); double r = 0.2; for (int v = 0; v < G.V(); v++) { if (v < M) { StdDraw.circle((double) v, 0.0, r); StdDraw.text((double) v, 0.0, (new Character(re[v]).toString())); } else StdDraw.filledCircle((double) v, 0.0, r); } StdDraw.setPenColor(StdDraw.RED); StdDraw.setPenRadius(0.005); ops = new Stack<Integer>(); for (int i = 0; i < M; i++) { int lp = i; if (re[i] == '(' || re[i] == '|') ops.push(i); else if (re[i] == ')') { int or = ops.pop(); if (re[or] == '|') { lp = ops.pop(); G.addEdge(lp, or + 1); G.addEdge(or, i); // connect (lp, or+1), (or, i) double level = (double) levels[or]; StdDraw.line(or, r, or, level); StdDraw.line(or, level, i, level); StdDraw.arrow(i, level, i, r); StdDraw.line(lp, -r, lp, -level); StdDraw.line(lp, -level, or + 1, -level); StdDraw.filledArrow(or + 1, -level, or + 1, -r); } else lp = or; // 此时是'(', 在后面跟的是*时会用到 } if (i < M - 1 && re[i + 1] == '*') { // look ahead G.addEdge(lp, i + 1); G.addEdge(i + 1, lp); // connect (lp, i+1), (i+1, lp) double level = (double) levels[i + 1]; StdDraw.line(i + 1, r, i + 1, level); StdDraw.line(i + 1, level, lp, level); StdDraw.filledArrow(lp, level, lp, r); StdDraw.line(lp, -r, lp, -level); StdDraw.line(lp, -level, i + 1, -level); StdDraw.filledArrow(i + 1, -level, i + 1, -r); } if (re[i] == '(' || re[i] == '*' || re[i] == ')') { G.addEdge(i, i + 1); StdDraw.filledArrow(i + r, 0.0, i + 1 - r, 0.0); } else if (re[i] != '|') { // match transitions StdDraw.setPenColor(); StdDraw.filledArrow(i + r, 0.0, i + 1 - r, 0.0); StdDraw.setPenColor(StdDraw.RED); } } }
public static void main(String[] args) { // GUI Variables int width = 1024; int height = 256; int pauseTime = 0; Font typeFont = new Font("Consolas", 1, 24); Font sentenceFont = new Font("Consolas", 0, 16); boolean multipleWords = false; // Strings for typing word, prediction, and sentence. StringBuilder typeString = new StringBuilder(); String prediction; StringBuilder sentence = new StringBuilder(); // Set up window StdDraw.setCanvasSize(width, height); StdDraw.setXscale(0.0, (double) width); StdDraw.setYscale(0.0, (double) height); // Train on files. WordPredictor predictor; if (args.length > 0) { try { int number = Integer.parseInt(args[args.length - 1]); if (number > 0 && number <= 9) { predictor = new WordPredictor(number); multipleWords = true; } else { predictor = new WordPredictor(); } } catch (NumberFormatException e) { predictor = new WordPredictor(); predictor.train(args[args.length - 1]); } for (int i = 0; i < args.length - 1; i++) { predictor.train(args[i]); } predictor.build(); } else { predictor = new WordPredictor(); } // Main loop for GUI and typing. while (true) { // Clear screen and reset variables. StdDraw.clear(); ArrayList<Word> best = null; if (predictor.getBest(typeString.toString()) != null) { best = predictor.getBest(typeString.toString()).getList(); } // If next key has been typed then act. if (StdDraw.hasNextKeyTyped()) { char key = StdDraw.nextKeyTyped(); // If key is a-z or apostrophe add it to string. if ((key >= 'a' && key <= 'z') || key == '\'') { typeString.append(key); } else if (key == ' ') { // If space move word to sentence, train into dict and reset if (typeString.length() > 0) { sentence.append(typeString + " "); predictor.trainWord(typeString.toString()); predictor.build(); typeString = null; typeString = new StringBuilder(); } } else if (key == '\b') { // Remove last letter if possible. if (typeString.length() > 0) { typeString.deleteCharAt(typeString.length() - 1); } } else if (key == '\n') { if (best != null) { // If enter was pressed and there is a prediction // Use the prediction and reset current word. sentence.append(best.get(0).getWord() + " "); typeString = null; typeString = new StringBuilder(); predictor.trainWord(best.get(0).getWord()); predictor.build(); } } else if (multipleWords) { // If there are multiple words available listen for numbers // These will allow for adding from suggestions. if (key >= '1' && key <= '9' && best != null) { int predNum = Integer.parseInt(key + ""); if (best.size() >= predNum) { sentence.append(best.get(predNum - 1).getWord() + " "); typeString = null; typeString = new StringBuilder(); predictor.trainWord(best.get(predNum - 1).getWord()); predictor.build(); } } } } // Start redrawing onto screen. // Typed word set up and print. StdDraw.setPenColor(Color.BLACK); StdDraw.setFont(typeFont); StdDraw.text(width / 2, height / 4 * 3, typeString.toString()); // Show prediction if it exists. if (best != null) { StdDraw.setPenColor(Color.BLUE); for (int i = 0; i < best.size(); i++) { StdDraw.text( width * ((i + .5) / best.size()), height / 2 + typeFont.getSize() / 2, best.get(i).getWord()); if (multipleWords) { StdDraw.text( width * ((i + .5) / best.size()), height / 2 - typeFont.getSize() / 2, "" + (i + 1)); } } StdDraw.setPenColor(Color.BLACK); } // Print out the sentence. StdDraw.setFont(sentenceFont); StdDraw.textRight(width - 20, height / 4, sentence.toString()); // Pause the loop based on time set at beginning. Set to 0. StdDraw.show(pauseTime); } }