-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathViewerText.java
92 lines (79 loc) · 2.62 KB
/
ViewerText.java
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
import java.io.*;
import java.util.*;
//********************************************************************
// ViewerText.java @version 1.01
// Text-based game state viewer (to file).
// Copyright (c) 2012 Daniel R. Collins. All rights reserved.
// See the bottom of this file for any licensing information.
//********************************************************************
public class ViewerText implements ViewerInterface {
//-----------------------------------------------------------------
// Fields
//-----------------------------------------------------------------
PrintWriter printer;
//-----------------------------------------------------------------
// Open
//-----------------------------------------------------------------
public void open () throws IOException {
printer = new PrintWriter(new File("output.txt"), "UTF-8");
}
//-----------------------------------------------------------------
// Close
//-----------------------------------------------------------------
public void close () {
printer.close();
}
//-----------------------------------------------------------------
// Update
//-----------------------------------------------------------------
public void update (GameState gs) {
// First row: top of deck, waste, foundations
print(gs.deck());
print(gs.waste());
print(" ");
for (int i = 0; i < 4; i++) {
print(gs.found(i));
}
print("\r\n");
// Find max length of tables
int maxTable = 0;
for (int i = 0; i < 7; i++) {
int size = gs.table(i).size();
if (size > maxTable) maxTable = size;
}
// Print a row for each
for (int i = 0; i < maxTable; i++) {
for (int j = 0; j < 7; j++) {
if (i < gs.table(j).size())
print(gs.table(j).get(i));
else
print(" ");
}
print("\r\n");
}
print("\r\n");
}
//-----------------------------------------------------------------
// Print top of pile
//-----------------------------------------------------------------
void print (Pile pile) {
if (pile.isEmpty()) {
print("-- ");
}
else {
print(pile.getTopCard());
}
}
//-----------------------------------------------------------------
// Print one card
//-----------------------------------------------------------------
void print (Card card) {
print(card.isFaceUp() ? card.toString() + " " : "[] ");
}
//-----------------------------------------------------------------
// Print shortcut
//-----------------------------------------------------------------
void print (String s) {
printer.print(s);
}
}