-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
134 lines (117 loc) · 5.11 KB
/
Copy pathGame.java
File metadata and controls
134 lines (117 loc) · 5.11 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
import java.util.ArrayList;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
import java.util.HashMap;
// To wipe screen:
// System.out.print("\033[H\033[2J");
// System.out.flush();
public class Game {
static HashMap<String, Word> cmupron;
static ArrayList<Poem> poems;
public static void main(String[] args) {
readCMUPron("cmupron.txt");
readPoems("Shakepeare.txt");
Scanner scanner = new Scanner(System.in);
System.out.print("Do you want to play easy or hard:");
if (scanner.nextLine().equals("easy")) {
playGameEasy();
} else {
playGame();
}
}
static void readCMUPron(String cmu) { // Fills the cmupron field with the dictionary of cmupron info
try {
cmupron = new HashMap<String, Word>();
FileReader f = new FileReader(cmu);
BufferedReader reader = new BufferedReader(f);
String cmuline = reader.readLine();
while (cmuline != null) {
cmuline.trim();
Word x = new Word(cmuline);
cmupron.put(x.word.toLowerCase(), x);
cmuline = reader.readLine();
}
} catch (IOException e) {
System.out.format("IOException %s\n", e);
}
}
static void readPoems(String poemfile) { // fills the poems list with all of the shakespeare sonnets
try {
poems = new ArrayList<Poem>();
FileReader f = new FileReader(poemfile);
BufferedReader reader = new BufferedReader(f);
String poemLine = reader.readLine();
while (poemLine != null) {
ArrayList<Integer> theusablelines = new ArrayList<Integer>();
if (poemLine.length() < 10) {
poemLine = reader.readLine();
continue;
}
String wholePoem = "";
for (int i = 0; i < 14; i++) { // Loops 14 times for each line of a sonnet
wholePoem += poemLine + "\n";
if (cmupron.get(Poem.getLastWord(poemLine)) != null) { // Gets every usable line, lines that can be checked for rhyme
theusablelines.add(i);
}
poemLine = reader.readLine();
}
Poem newPoem = new Poem(wholePoem);
newPoem.usablelines = theusablelines;
poems.add(newPoem);
poemLine = reader.readLine();
}
} catch (IOException f) {
System.out.format("IOException %s\n", f);
}
}
static void playGame() {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int index = random.nextInt(poems.size());
Poem gamepoem = poems.get(index);
int randlistnumber = random.nextInt(gamepoem.usablelines.size());
int linenumber = gamepoem.usablelines.get(randlistnumber);
System.out.print("To Begin Game, Type Play:");
String beginword = scanner.nextLine().toLowerCase().trim();
if (beginword.equals("play")) {
System.out.println("Here's how the game works: ");
System.out.print(
" You will be given a random shakepeare sonnet \n To test out your poet skills you will be asked to come up with a line\n");
System.out.println("Here we go!");
System.out.print("Type ready when ready: ");
String readyword = scanner.nextLine().toLowerCase().trim();
if (readyword.equals("ready")) {
System.out.print("\n\n\n");
gamepoem.takeOutLine(linenumber).printPoem();
}
System.out.print("\n\n What line shall we put?:");
String inputline = scanner.nextLine().toLowerCase().replaceAll("[)(\\[\\]!,.?{}:;\"\\'\\-]", "");
if (gamepoem.lineFits(inputline, linenumber)) {
System.out.println("Yes! You did it! You are smarter then Shakespeare!");
}
} else {
System.out.println("Aight, I see how it is");
}
}
static void playGameEasy() {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
Poem gamepoem = poems.get(random.nextInt(poems.size()));
int randlistnumber = random.nextInt(gamepoem.usablelines.size());
int linenumber = gamepoem.usablelines.get(randlistnumber);
gamepoem.replaceLastWordOfLineWith(linenumber, "_____").printPoem();
System.out.print("\n\n\nWhat word shall we put in the blank?:");
String inputword = scanner.nextLine();
Poem playerpoem = gamepoem.replaceLastWordOfLineWith(linenumber, inputword);
if (gamepoem.lineRhymes(playerpoem.poem.get(linenumber), linenumber)) {
System.out.println("Your word worked! Great Job!");
} else {
System.out.println("Sorry! That did not work! Better luck next time sweetie!");
}
}
}