例子: Letters of player 1 : a c f h i n p r s w Enter your word (or ‘@’ to pass or ‘!’ to quit) : saw Total score for word : 6 Total score for game : 6
Letters of player 2 : b c e g n n o t x z Enter your word (or ‘@’ to pass or ‘!’ to quit): bench Error : A valid word is formed but one or more letter(s) used are not yours Enter your word (or ‘@’ to pass or ‘!’ to quit): benc Error : An invalid word is formed Enter your word (or ‘@’ to pass or ‘!’ to quit): bet Total score for word : 5 Total score for game : 5
Letters of player 1 : c e f g h i n p r t Enter your word (or ‘@’ to pass or ‘!’ to quit): @ Total score for word : 0 Total score for game : 6
Letters of player 2 : a c i g n n o v x z Enter your word (or ‘@’ to pass or ‘!’ to quit): gain Total score for word : 5 Total score for game : 10
Letters of player 1 : c e f g h i n p r t Enter your word (or ‘@’ to pass or ‘!’ to quit): !
Total score for player 1: 6 Total score for player 2: 10 Player 2 won!
public class Words { private static char[] consonant={'B','C','D','F','G','H','J','K','L','M','N','P','Q', 'R','S','T','V','W','X','Y','Z'}; private static char[] vowel={'A','E','I','O','U'}; private static ArrayList<String> dict = new ArrayList<String>();
public static void main(String[] args) throws IOException { // TODO Auto-generated method stub getResource(); int score1 = 0; int score2 = 0; Scanner userInput = new Scanner(System.in); String A= null; int count = 0; boolean flag = false;//true 为Player1, FALSE为Player2 do{ String random = randomString(); System.out.println(random); if(flag) { System.out.println("Player1 input a word: (or ‘@’ to pass or ‘!’ to quit)"); A=userInput.nextLine(); if(A.equals("@") ) { flag =!flag; System.out.println("Player2 input a word: (or ‘@’ to pass or ‘!’ to quit)"); A =userInput.nextLine(); } } else { System.out.println("Player2 input a word: (or ‘@’ to pass or ‘!’ to quit)"); A=userInput.nextLine(); if(A.equals("@") ) { flag = !flag; System.out.println("Player1 input a word: (or ‘@’ to pass or ‘!’ to quit)"); A =userInput.nextLine(); } } if(A.equals("@")) { System.out.println("反复切换,系统退出"); return; } else { String temp = null; if(flag) { //String random = randomString(); temp = match(random,A); if(find(temp,dict)) score1+=2; System.out.println(" Player1 get "+score1+" points"); } else { //String random = randomString(); temp = match(random,A); if(find(temp,dict)) score2+=2;; System.out.println(" Player2 get "+score2+" points"); }