Answer the question
In order to leave comments, you need to log in
Decided to try writing a wordplay in Java, how do I solve the problem?
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Game {
private List <String> Words = new LinkedList();//Все слова игры
Iterator <String> iterator = Words.iterator();
private String word = "";//Слово
private char last;//Последняя буква последнего слова
public Game(String i){
this.StartGame(i);
}
private void StartGame(String i){
System.out.println("Что бы во время игры войти в меню надо ввести !");
System.out.println("Игра пошла, введите 1 слово");
if(i.equals("1")) this.StartPeopleGame();
else this.StartPcGame();
}
private void StartPeopleGame() {
System.out.println("-----Играют люди-----");
while(true){
//---------------------------------
Scanner sc = new Scanner(System.in, "cp1251");
word = sc.nextLine();
//----------------------------------
String check = this.CheckWord();
if(!check.equals("Normal")) {
System.out.println(check);
continue;
}
Words.add(word);
//-----------------------------------
if(this.LastCharacter()=='\\'){
System.out.println("Такого слова точно не сущевствует!");
Words.remove(word);
continue;
}
last = this.LastCharacter();
System.out.println("Вам на "+last);
//-----------------------------------
}
}
private void StartPcGame() {
System.out.println("-----Играют человек с компьютером-----");
}
private String CheckWord(){
char[] wordChar = word.toCharArray();
if(Words.isEmpty()) return "Normal";
if(wordChar[0]!=last){
return "Cлово не начинается с буквы "+ this.last;
}
while(iterator.hasNext()){
if(Words.indexOf(word)!=-1) return "Данное слово уже было введено";
}
return "Normal";
}
private char LastCharacter(){
char[] wordChar = word.toCharArray();
int i = wordChar.length;
if(i==0) return '\\';
if(i==1 && (wordChar[i-1]=='ь' || wordChar[i-1]=='ъ' || wordChar[i-1]=='ы' ||wordChar[i-1]=='й')) return '\\';
if(i==1) return wordChar[i-1];
if((wordChar[i-1]=='ь' || wordChar[i-1]=='ъ' || wordChar[i-1]=='ы' ||wordChar[i-1]=='й')&&(wordChar[i-1]=='ь' || wordChar[i-1]=='ъ' || wordChar[i-1]=='ы' ||wordChar[i-1]=='й')) return wordChar[i-1];
return wordChar[i-1];
}
}
Answer the question
In order to leave comments, you need to log in
I hope this isn't homework. Here's a simplified and slightly clumsy version of the game. Modify it to your needs yourself - just too lazy to pervert much :)
And a couple of comments: In Java, it is customary to write the names of variables (except for constants) and methods with a small letter. IMHO Always enclose the if block in brackets. Even if it's one line of code.
package wordgame;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Game {
public static void main(String[] args) {
new Game().run();
}
private static final char[] VALID_CHARACTERS = new char[] { 'а', 'б', 'в',
'г', 'д', 'е', 'ж', 'з', 'и', 'к', 'л', 'м', 'н', 'о', 'п', 'р',
'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'щ', 'э', 'ю', 'я' };
private Set<String> words;
private int playerNumber;
private Scanner scanner;
private Character playerWordLastChar;
public void run() {
words = new HashSet<String>();
scanner = new Scanner(System.in, "cp1251");
playerNumber = 1;
System.out.println("Игра началась! \nВведите слово!\nДля подтверждения своего ответа нажмите \"ENTER\"");
nextPlayerTurn();
}
private void nextPlayerTurn() {
System.out.println("\nХод игрока " + playerNumber + "\n");
while (!submitTurn()) {
}
playerNumber = playerNumber == 1 ? 2 : 1;
nextPlayerTurn();
}
private boolean submitTurn() {
String word = scanner.nextLine();
if (word.isEmpty()) {
System.out.println("Введите слово!");
return false;
}
char firstChar = word.charAt(0);
char lastChar = word.charAt(word.length() - 1);
boolean submit = false;
if(words.contains(word)) {
System.out.println("Такое слово уже было!");
}
else if (playerWordLastChar != null && firstChar != playerWordLastChar) {
System.out.println("Слово должно начинаться на \"" + playerWordLastChar +"\"");
} else if (!isLastCharValid(lastChar)) {
System.out.println("Слово не должно оканчиваться на: \"" + lastChar+ "\"");
} else {
words.add(word);
playerWordLastChar = lastChar;
submit = true;
}
return submit;
}
private boolean isLastCharValid(char lastChar) {
for (char ch : VALID_CHARACTERS) {
if (ch == lastChar) {
return true;
}
}
return false;
}
}
Instead of giving the whole class, they would describe the algorithm. In general, it looks like you have output from several streams at the same time, hence the leapfrog with letters. Well, there is such a thing as a debugger available in any IDE, even in emacs, not to mention eclipse, netbins and idea. Just step through the algorithm and set breakpoints at incomprehensible places.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question