220 - Othello java
阿新 • • 發佈:2018-12-21
這題終於ac了,格式上有個大坑,如下:
Black - 7 White - 6,這個東西如果數字是1位數,前面就是2個空格,如果是2位數,前面就是1個空格....題目又不說,多虧是照著udebug設定的格式,要不永遠都ac不了 System.out.println("Black - "+String.format("%2d",cha[1])+" White - "+String.format("%2d",cha[0]));
下面是ac的程式碼,下面是最開始存在格式錯誤的程式碼
import java.util.ArrayList; import java.util.Scanner; public class Main { static char[][] board; static char current; static Scanner sc = new Scanner(System.in); //上下左右,左上,右上,左下,右下 static int[] xArr = {0,0,-1,1,-1,1,-1,1}; static int[] yArr = {-1,1,0,0,-1,-1,1,1}; public static void main(String[] args) { int m = Integer.parseInt(sc.nextLine()); while(m-->0){ //把輸入資料轉換成 二維字元陣列 board = new char[11][11]; for (int i = 0; i < 8; i++) { board[i] = sc.nextLine().toCharArray(); } //當前是哪方下子 current = sc.nextLine().charAt(0); //存當前什麼操作的字元陣列 char[] ml = new char[3]; while((ml=sc.nextLine().toCharArray())[0]!='Q'){ if(ml[0]=='L'){ //呼叫l方法 ArrayList<String> list = l(); if(list.size()==0){ System.out.println("No legal move."); }else{ for (int i = 0; i < list.size(); i++) { if(i==list.size()-1) System.out.print(list.get(i).trim()); else System.out.print(list.get(i)); } System.out.println(); } }else if (ml[0]=='M'){ //呼叫m方法 m(ml); } } pr(m); if(m!=0) System.out.println(); } } public static ArrayList l(){ ArrayList<String> strList = new ArrayList<>(); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (board[i][j]!='-') continue; w3:for (int k = 0; k < 8; k++) { int ii=i,jj=j; ii+=xArr[k]; jj+=yArr[k]; int count =1;//輔助判斷 while(ii>=0 && ii<=7 && jj>=0 && jj<=7){ if(board[ii][jj] == '-') break; else if(board[ii][jj]!=current){ ii+=xArr[k]; jj+=yArr[k]; count++; } else if (board[ii][jj]==current){ if(count >=2){ strList.add("("+(i+1)+","+(j+1)+") "); break w3; }else{ break; } } } } } } return strList; } public static void m(char[] ml){ //呼叫l方法,求合法位置 ArrayList list = l(); String str1 = "("+ml[1]+","+ml[2]+") "; //沒有合法操作,就把current換成另一方 if(!list.contains(str1)){ current = current=='W'?'B':'W'; } //放下棋子,改變其他顏色的棋子 int x=ml[1]-49; int y=ml[2]-49; board[x][y] = current; for (int i = 0; i < 8; i++) { int ii=x,jj=y; ii+=xArr[i]; jj+=yArr[i]; int count =1;//輔助判斷 while(ii>=0 && ii<=7 && jj>=0 && jj<=7){ if(board[ii][jj] == '-') break; else if(board[ii][jj]!=current){ ii+=xArr[i]; jj+=yArr[i]; count++; } else if (board[ii][jj]==current){ if(count >=2){ // strList.add("("+(i+1)+","+(j+1)+") "); //這個方向的i是可行的,更改棋子,計算數量 int i2 = x,j2 = y; while (board[i2+=xArr[i]][j2+=yArr[i]]!=current){ board[i2][j2] = current; } break; }else{ break; } } } } int[] cha = cha(); System.out.println("Black - "+String.format("%2d",cha[1])+" White - "+String.format("%2d",cha[0])); //改current current = current=='W'?'B':'W'; } public static int[] cha(){ int[] arr = new int[2]; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (board[i][j]=='W') arr[0]++; else if (board[i][j]=='B') arr[1]++; } } return arr; } //列印棋盤 public static void pr(int m){ for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { System.out.print(board[i][j]); } System.out.println(); } } }
提交uva ,報錯Presentation error...這種錯誤的意思是輸出結果對的,但格式有誤,可能多個空格,少個回車什麼的.改了半天,還是報這個錯,網上的程式碼都是用c寫的,也沒找到java的,沒辦法了,先把程式碼貼這吧,以後有機會了在研究
package com.uva; import java.util.ArrayList; import java.util.Scanner; public class Othello { static char[][] board; static char current; static Scanner sc = new Scanner(System.in); //上下左右,左上,右上,左下,右下 static int[] xArr = {0,0,-1,1,-1,1,-1,1}; static int[] yArr = {-1,1,0,0,-1,-1,1,1}; public static void main(String[] args) { int m = Integer.parseInt(sc.nextLine()); while(m-->0){ //把輸入資料轉換成 二維字元陣列 board = new char[11][11]; for (int i = 0; i < 8; i++) { board[i] = sc.nextLine().toCharArray(); } //當前是哪方下子 current = sc.nextLine().charAt(0); //存當前什麼操作的字元陣列 char[] ml = new char[3]; while((ml=sc.nextLine().toCharArray())[0]!='Q'){ if(ml[0]=='L'){ //呼叫l方法 ArrayList<String> list = l(); if(list.size()==0){ System.out.println("No legal move."); }else{ for (int i = 0; i < list.size(); i++) { if(i==list.size()-1) System.out.print(list.get(i).trim()); else System.out.print(list.get(i)); } System.out.println(); } }else if (ml[0]=='M'){ //呼叫m方法 m(ml); } } pr(); if(m!=0) System.out.println(); } } public static ArrayList l(){ ArrayList<String> strList = new ArrayList<>(); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (board[i][j]!='-') continue; w3:for (int k = 0; k < 8; k++) { int ii=i,jj=j; ii+=xArr[k]; jj+=yArr[k]; int count =1;//輔助判斷 while(ii>=0 && ii<=7 && jj>=0 && jj<=7){ if(board[ii][jj] == '-') break; else if(board[ii][jj]!=current){ ii+=xArr[k]; jj+=yArr[k]; count++; } else if (board[ii][jj]==current){ if(count >=2){ strList.add("("+(i+1)+","+(j+1)+") "); break w3; }else{ break; } } } } } } return strList; } public static void m(char[] ml){ //呼叫l方法,求合法位置 ArrayList list = l(); String str1 = "("+ml[1]+","+ml[2]+") "; //沒有合法操作,就把current換成另一方 if(!list.contains(str1)){ current = current=='W'?'B':'W'; } //放下棋子,改變其他顏色的棋子 int x=ml[1]-49; int y=ml[2]-49; board[x][y] = current; for (int i = 0; i < 8; i++) { int ii=x,jj=y; ii+=xArr[i]; jj+=yArr[i]; int count =1;//輔助判斷 while(ii>=0 && ii<=7 && jj>=0 && jj<=7){ if(board[ii][jj] == '-') break; else if(board[ii][jj]!=current){ ii+=xArr[i]; jj+=yArr[i]; count++; } else if (board[ii][jj]==current){ if(count >=2){ // strList.add("("+(i+1)+","+(j+1)+") "); //這個方向的i是可行的,更改棋子,計算數量 int i2 = x,j2 = y; while (board[i2+=xArr[i]][j2+=yArr[i]]!=current){ board[i2][j2] = current; } break; }else{ break; } } } } int[] cha = cha(); System.out.println("Black - "+cha[1]+" White - "+cha[0]); //改current current = current=='W'?'B':'W'; } public static int[] cha(){ int[] arr = new int[2]; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (board[i][j]=='W') arr[0]++; else if (board[i][j]=='B') arr[1]++; } } return arr; } //列印棋盤 public static void pr(){ for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { System.out.print(board[i][j]); } System.out.println(); } } } /* Othello is a game played by two people on an 8 x 8 board, using disks that are white on one side and black on the other. One player places disks with the white side up and the other player places disks with the black side up. The players alternate placing one disk on an unoccupied space on the board. In placing a disk, the player must bracket at least one of the other color disks. Disks are bracketed if they are in a straight line horizontally, vertically, or diagonally, with a disk of the current player’s color at each end of the line. When a move is made, all the disks that were bracketed are changed to the color of the player making the move. (It is possible that disks will be bracketed across more than one line in a single move.) On the left Legal Moves for White (2,3),(3,3),(3,5),(3,6) (6,2),(7,3),(7,4),(7,5) On the right Board Configuration after White Moves to (7,3) Write a program to read a series of Othello games. Input The first line of the input is the number of games to be processed. Each game consists of a board configuration followed by a list of commands. The board configuration consists of 9 lines. The first 8 specify the current state of the board. Each of these 8 lines contains 8 characters, and each of these characters will be one of the following: ‘-’ indicating an unoccupied square ‘B’ indicating a square occupied by a black disk ‘W’ indicating a square occupied by a white disk The ninth line is either a ‘B’ or a ‘W’ to indicate which is the current player. You may assume that the data is legally formatted. Then a set of commands follows. The commands are to list all possible moves for the current player, make a move, or quit the current game. There is one command per line with no blanks in the input. Output The commands and the corresponding outputs are formatted as follows: List all possible moves for the current player. The command is an ‘L’ in the first column of the line. The program should go through the board and print all legal moves for the current player in the format (x, y) where x represents the row of the legal move and y represents its column. These moves should be printed in row major order which means: 1) all legal moves in row number i will be printed before any legal move in row number j if j is greater than i and 2) if there is more than one legal move in row number i, the moves will be printed in ascending order based on column number. All legal moves should be put on one line. If there is no legal move because it is impossible for the current player to bracket any pieces, the program should print the message ‘No legal move.’ Make a move. The command is an ‘M’ in the first column of the line, followed by 2 digits in the second and third column of the line. The digits are the row and the column of the space to place the piece of the current player’s color, unless the current player has no legal move. If the current player has no legal move, the current player is first changed to the other player and the move will be the move of the new current player. You may assume that the move is then legal. You should record the changes to the board, including adding the new piece and changing the color of all bracketed pieces. At the end of the move, print the number of pieces of each color on the board in the format ‘Black - xx White - yy’ where xx is the number of black pieces on the board and yy is the number of white pieces on the board. After a move, the current player will be changed to the player that did not move. Quit the current game. The command will be a ‘Q’ in the first column of the line. At this point, print the final board configuration using the same format as was used in the input. This terminates input for the current game. You may assume that the commands will be syntactically correct. Put one blank line between output from separate games and no blank lines anywhere else in the output. Sample Input 2 -------- -------- -------- ---WB--- ---BW--- -------- -------- -------- W L M35 L Q WWWWB--- WWWB---- WWB----- WB------ -------- -------- -------- -------- B L M25 L Q Sample Output (3,5) (4,6) (5,3) (6,4) Black - 1 White - 4 (3,4) (3,6) (5,6) -------- -------- ----W--- ---WW--- ---BW--- -------- -------- -------- No legal move. Black - 3 White - 12 (3,5) WWWWB--- WWWWW--- WWB----- WB------ -------- -------- -------- -------- 10 -------- -------- -------- ---WB--- ---BW--- -------- -------- -------- W L M46 L M34 L M23 L M47 L M56 L M57 L M43 L M33 L M63 L M64 Q -------- -------- -------- ---WB--- ---BW--- -------- -------- -------- B L M56 L M64 L M33 L M57 L M74 L M53 L M42 L M84 L M63 L M34 Q -------- -------- -------- ---WB--- ---BW--- -------- -------- -------- W L M64 L M63 L M35 L M34 L M62 L M26 L M43 L M52 L M23 L M25 Q -------- -------- -------- ---WB--- ---BW--- -------- -------- -------- W L M35 L M56 L M64 L M53 L M63 L M25 L M46 L M73 L M66 L M57 Q -------- -------- -------- ---WB--- ---BW--- -------- -------- -------- W L M53 L M63 L M35 L M52 L M51 L M41 L M31 L M36 L M64 L M34 Q -------- -------- -------- ---WB--- ---BW--- -------- -------- -------- B L M34 L M33 L M65 L M46 L M36 L M66 L M43 L M53 L M22 L M23 Q -------- -------- -------- ---WB--- ---BW--- -------- -------- -------- W L M53 L M65 L M46 L M52 L M75 L M76 L M63 L M35 L M41 L M47 Q -------- -------- -------- ---WB--- ---BW--- -------- -------- -------- B L M43 L M33 L M34 L M35 L M25 L M53 L M36 L M24 L M63 L M65 Q -------- -------- -------- ---WB--- ---BW--- -------- -------- -------- B L M34 L M53 L M66 L M56 L M67 L M35 L M52 L M33 L M36 L M62 Q -------- -------- -------- ---WB--- ---BW--- -------- -------- -------- W L M64 L M43 L M35 L M65 L M75 L M26 L M42 L M36 L M46 L M56 Q (3,5) (4,6) (5,3) (6,4) Black - 1 White - 4 (3,4) (3,6) (5,6) Black - 3 White - 3 (2,3) (3,3) (4,3) (5,3) (6,3) Black - 2 White - 5 (2,4) (3,6) (4,7) (5,6) (6,6) Black - 5 White - 3 (3,3) (3,5) (3,7) (5,3) (5,6) (6,4) Black - 4 White - 5 (2,4) (3,6) (5,7) (6,4) (6,5) (6,6) Black - 7 White - 3 (4,3) (4,8) (6,3) (6,4) (6,5) (6,7) Black - 6 White - 5 (1,2) (2,4) (3,2) (3,3) (3,5) (3,6) (4,2) Black - 8 White - 4 (3,2) (4,8) (6,3) (6,4) (6,5) (6,7) Black - 7 White - 6 (1,2) (1,3) (2,4) (3,5) (4,2) (5,3) (6,4) Black - 9 White - 5 -------- --W----- --BW---- --WBWBB- ---BBBB- --WB---- -------- -------- (3,4) (4,3) (5,6) (6,5) Black - 4 White - 1 (4,6) (6,4) (6,6) Black - 3 White - 3 (3,3) (4,3) (5,3) (6,3) (7,3) Black - 5 White - 2 (3,4) (3,6) (4,6) (5,7) Black - 3 White - 5 (6,3) (6,5) (6,6) (6,7) (7,4) Black - 6 White - 3 (2,2) (3,4) (3,5) (5,3) (7,3) Black - 5 White - 5 (4,2) (4,6) (6,2) (6,3) (6,5) (6,6) (6,7) Black - 7 White - 4 (2,2) (3,4) (3,5) (3,6) (5,2) (7,3) (8,4) Black - 5 White - 7 (5,8) (6,3) (6,5) (6,6) (6,7) (7,5) Black - 7 White - 6 (2,2) (3,1) (3,4) (3,5) (5,2) (6,2) Black - 4 White - 10 -------- -------- --BW---- -B-WW--- --BWWWW- --BW---- ---W---- ---W---- (3,5) (4,6) (5,3) (6,4) Black - 1 White - 4 (4,3) (6,3) (6,5) Black - 3 White - 3 (3,5) (4,6) (5,3) (6,2) Black - 2 White - 5 (3,4) (3,6) (5,6) (6,5) (7,4) Black - 4 White - 4 (2,3) (2,4) (3,3) (4,3) (5,3) (6,2) (7,2) Black - 3 White - 6 (2,6) (3,6) (4,6) (5,6) (6,6) (7,2) (7,4) Black - 5 White - 5 (2,3) (2,4) (2,5) (3,3) (4,3) (5,3) Black - 4 White - 7 (3,2) (3,6) (5,2) (5,3) (5,6) (6,5) (7,2) (7,4) Black - 6 White - 6 (1,7) (2,3) (2,4) (2,5) (4,1) (4,2) (5,3) Black - 5 White - 8 (2,4) (2,5) (3,3) (3,6) (4,6) (5,3) (5,6) (6,5) (7,2) (7,4) Black - 7 White - 7 -------- --W-BB-- ---BB--- --BWW--- -B-BW--- -WWW---- -------- -------- (3,5) (4,6) (5,3) (6,4) Black - 1 White - 4 (3,4) (3,6) (5,6) Black - 3 White - 3 (6,3) (6,4) (6,5) (6,6) (6,7) Black - 2 White - 5 (2,5) (3,3) (3,4) (5,3) (7,3) Black - 4 White - 4 (4,2) (4,6) (6,2) (6,3) (6,5) (6,6) (6,7) Black - 3 White - 6 (2,5) (2,6) (3,3) (3,4) (7,3) (7,5) Black - 6 White - 4 (2,6) (3,6) (4,2) (4,3) (4,6) (5,2) (5,7) (6,2) (6,6) Black - 4 White - 7 (3,4) (3,6) (5,7) (6,5) (7,3) (7,5) Black - 6 White - 6 (1,5) (2,4) (2,6) (4,2) (5,2) (5,7) (6,2) (6,6) (6,7) (7,2) (8,2) Black - 5 White - 8 (3,6) (3,7) (5,7) (6,5) (7,5) Black - 10 White - 4 -------- ----B--- ----B--- ---WWB-- --BBBBB- --BW-W-- --B----- -------- (3,5) (4,6) (5,3) (6,4) Black - 1 White - 4 (4,3) (6,3) (6,5) Black - 3 White - 3 (3,5) (4,6) (6,4) (7,3) Black - 2 White - 5 (3,4) (3,6) (4,3) (5,2) (5,6) Black - 4 White - 4 (5,1) (6,2) (6,4) (7,2) Black - 1 White - 8 (3,6) (4,1) (4,3) Black - 3 White - 7 (3,1) (7,2) (7,3) Black - 2 White - 9 (3,6) (4,3) (5,6) Black - 5 White - 7 (3,7) (4,6) (6,4) (7,3) (7,4) Black - 4 White - 9 (2,5) (3,4) (4,3) (5,6) (6,5) Black - 6 White - 8 -------- -------- W--BBB-- W--WB--- WBWWW--- --BW---- -------- -------- (3,4) (4,3) (5,6) (6,5) Black - 4 White - 1 (3,3) (3,5) (5,3) Black - 3 White - 3 (3,2) (4,3) (5,6) (6,5) Black - 5 White - 2 (2,4) (3,5) (4,6) (6,4) (6,6) Black - 4 White - 4 (2,2) (3,2) (3,5) (3,6) (3,7) (5,6) Black - 6 White - 3 (2,4) (2,6) (3,5) (6,4) (6,6) Black - 5 White - 5 (3,2) (4,3) (4,7) (5,6) (6,7) Black - 7 White - 4 (2,6) (3,5) (4,2) (5,3) (6,4) (7,5) Black - 5 White - 7 (2,2) (3,2) (4,2) (4,7) (5,2) (5,6) (6,2) (6,3) (6,4) (6,7) (7,7) Black - 7 White - 6 (1,1) (2,3) (2,4) (2,5) (2,6) (2,7) (3,5) (6,4) (7,5) (7,6) Black - 6 White - 8 -------- -BW----- --WB-B-- --WBBW-- --WWW--- ----BW-- -------- -------- (3,5) (4,6) (5,3) (6,4) Black - 1 White - 4 (4,3) (6,3) (6,5) Black - 3 White - 3 (3,6) (4,6) (5,6) (6,6) (7,6) Black - 2 White - 5 (3,3) (3,5) (3,7) (4,3) (5,2) Black - 5 White - 3 (6,2) (6,3) (6,4) (6,6) (7,5) Black - 3 White - 6 (3,4) (3,5) (3,6) (5,6) (7,6) Black - 5 White - 5 (5,1) (6,2) (6,3) (6,4) (7,7) Black - 4 White - 7 (3,5) (4,3) (5,6) (7,3) (7,4) (8,5) Black - 8 White - 4 (2,4) (2,5) (3,4) (3,6) (4,1) (4,3) (5,1) (5,6) (6,4) (7,7) (8,7) Black - 7 White - 6 (3,7) (4,3) (4,7) (5,1) (5,7) (6,4) (7,2) (7,3) (7,4) (8,5) Black - 9 White - 5 -------- -------- ----B--- W--BBBB- -WBWB--- --W-B--- ----WB-- -------- (3,4) (4,3) (5,6) (6,5) Black - 4 White - 1 (3,3) (3,5) (5,3) Black - 3 White - 3 (2,3) (3,4) (5,6) (6,5) Black - 5 White - 2 (3,5) (5,3) Black - 3 White - 5 (2,2) (2,3) (2,4) (2,5) (2,6) (3,6) (4,6) (5,6) (6,6) Black - 5 White - 4 (1,5) (2,3) (4,2) (5,3) (6,3) Black - 2 White - 8 (3,2) (3,6) (5,2) (5,6) (6,4) (6,5) Black - 4 White - 7 (1,5) (1,6) (2,3) (2,4) (2,6) (2,7) (3,7) Black - 3 White - 9 (1,3) (2,3) (3,2) (5,2) (6,2) (6,3) (6,5) Black - 6 White - 7 (1,5) (1,6) (2,6) (3,7) (4,6) (5,6) (6,4) (6,5) (7,3) Black - 5 White - 9 -------- ---WB--- --WWBB-- --WWB--- --WWW--- --B-W--- -------- -------- (3,4) (4,3) (5,6) (6,5) Black - 4 White - 1 (3,3) (3,5) (5,3) Black - 3 White - 3 (6,2) (6,3) (6,4) (6,5) (6,6) Black - 5 White - 2 (2,4) (3,5) (3,6) (5,6) Black - 4 White - 4 (4,6) (6,2) (6,3) (6,4) (6,5) (6,7) Black - 6 White - 3 (2,4) (3,3) (3,5) (3,6) (5,7) (7,7) Black - 4 White - 6 (3,3) (3,6) (5,2) (6,4) Black - 8 White - 3 (2,3) (2,4) (3,3) (6,2) (6,3) (6,4) (6,5) (7,7) (7,8) Black - 7 White - 5 (2,2) (2,3) (2,4) (2,5) (2,6) (3,6) Black - 9 White - 4 (3,7) (4,6) (6,2) (6,4) (6,5) (7,7) (7,8) Black - 8 White - 6 -------- -------- --WWWB-- ---WB--- -BWBBB-- -W---BB- -------- -------- (3,5) (4,6) (5,3) (6,4) Black - 1 White - 4 (4,3) (6,3) (6,5) Black - 3 White - 3 (3,2) (3,3) (3,4) (3,5) (3,6) Black - 2 White - 5 (2,6) (4,6) (6,5) (6,6) (7,4) Black - 4 White - 4 (3,3) (3,4) (4,2) (5,3) (6,3) (6,6) (7,5) Black - 3 White - 6 (2,6) (3,6) (4,6) (5,6) (6,6) (7,4) (7,6) Black - 5 White - 5 (2,5) (3,2) (3,3) (3,4) (4,2) (5,3) (6,3) Black - 3 White - 8 (3,2) (3,4) (3,6) (5,3) (5,6) (7,4) (7,6) (8,5) Black - 5 White - 7 (1,7) (2,5) (4,6) (5,3) Black - 4 White - 9 (3,2) (3,4) (5,3) (5,6) (5,7) (7,4) (7,6) (8,5) Black - 7 White - 7 -------- -----B-- ----BB-- -WWWWB-- ---BBB-- ---WW--- ----W--- -------- */