Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
114 lines (99 loc) · 4.16 KB
/
Copy pathMain.java
File metadata and controls
114 lines (99 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Scanner;
/**
*
* @author samtu
*/
public class Main {
/**
* @param args the command line arguments
*/
public static String Welcome;
public static String RoundInfo;
public static void main(String[] args) throws FileNotFoundException {
//set up varaibles
WordGame.initialize();
Scanner input = new Scanner(System.in);
int currentRound = 0;
boolean run = true;
boolean guessing;
boolean containsUnderscore = true;
boolean correctGuess = false;
boolean alreadyGuessed = false;
Welcome = "Welcome to WordGame! (title is a work-in-progress)";
System.out.println(Welcome);
//code that runs the game
while(run) {
//set up variables
currentRound +=1;
guessing = true;
String word = WordGame.generateWord();
int length = word.length();
LinkedList<Character> wrongGuesses = new LinkedList<>();
//creates a LinkedList of underscores for each letter in the word
LinkedList<Character> wordStatus = new LinkedList<>();
for(int i=0; i<length; i++) {
wordStatus.add('_');
}
//displays round information for the user
RoundInfo = "Current round: Round " + currentRound +
"\nThe word to guess is " + wordStatus.size() + " letters: " + WordGame.listToString(wordStatus);
System.out.println(RoundInfo);
//The main program for prompting and recording player guesses.
//Uses the LinkedList to replace underscores with correct guesses.
while(guessing) {
correctGuess = false;
alreadyGuessed = false;
System.out.println("\nPlease guess a letter:");
//saves the player's guess as a character
char guess = input.next().charAt(0);
for(int i=0; i<length; i++) {
if(guess == word.charAt(i)) { //if player guesses correctly
wordStatus.set(i, guess);
correctGuess = true;
}else { //check if the letter has already been guessed
for(char letter : wrongGuesses) {
if(guess == letter) {
alreadyGuessed = true;
}
}
}
}
if(!correctGuess && !alreadyGuessed) {
wrongGuesses.add(guess);
}
System.out.println("\n" + WordGame.listToString(wordStatus));
System.out.println("Wrong guesses: " + WordGame.listToString(wrongGuesses));
//if the player gets 5 wrong guesses, the game has been lost :(
if(wrongGuesses.size() >= 5) {
guessing = false;
//print the results
WordGame.loseText(word, wrongGuesses, currentRound);
run = WordGame.askIfContinue(input, run, currentRound);
currentRound = 0;
break;
}
//checks to see if any letters have not been guessed yet
containsUnderscore = false;
for(int i=0; i<length; i++) {
if(wordStatus.get(i).equals('_')) {
containsUnderscore = true;
}
}
//if all letters have been guessed, the game has been won!
if(!containsUnderscore) {
guessing = false;
//print the results
WordGame.winText(word, wrongGuesses);
run = WordGame.askIfContinue(input, run, currentRound);
}
}
}
}
}