private static void draw( boolean[][] maze, boolean[][] solution, int rowS, int colS, int rowE, int colE) { StdDraw.clear(); StdDraw.setPenColor(StdDraw.BLACK); int N = maze[0].length; StdDraw.setXscale(-1, N); StdDraw.setYscale(-1, N); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (!maze[i][j]) StdDraw.filledSquare(j, N - i - 1, .5); StdDraw.setPenColor(StdDraw.RED); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { if (solution[i][j]) { StdDraw.filledSquare(j, N - i - 1, .4); } } StdDraw.setPenColor(StdDraw.GREEN); StdDraw.filledSquare(colS, N - rowS - 1, .4); StdDraw.setPenColor(StdDraw.YELLOW); StdDraw.filledSquare(colE, N - rowE - 1, .4); }
// 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"); }
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) { StdDraw.setXscale(0, 600); StdDraw.setYscale(0, 600); while (true) { Pankenski( new Point2D(300, 0), new Point2D(0, 300), new Point2D(600, 300), new Point2D(300, 600), 20); StdDraw.clear(); } }
/** 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); // 延时 }
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(); }
protected static void initGame() { StdDraw.clear(); StdDraw.setCanvasSize(Constants.X_VIEW, Constants.Y_VIEW); StdDraw.setXscale(0, Constants.X_MAX); StdDraw.setYscale(0, Constants.Y_MAX); }
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 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); } }