-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextBaduk.java
141 lines (131 loc) · 3.25 KB
/
TextBaduk.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
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
135
136
137
138
139
140
141
import java.util.Arrays;
import java.util.Scanner;
class TextBaduk
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int y = 0;
int x = 0;
int prevy;
int prevx;
char[] players = {'O', '0'};
int player = 0;
Stone [] point = new Stone[100];
for (int i = 0; i < 100; i++)
{
point[i] = new Stone();
}
Board.init(point);
while (true)
{
Board.print(point, players[player]);
System.out.printf("%d , %d = %c\n",x,y,point[x+(y*10)].character);
prevx = x;
prevy = y;
do //Continue to get input until it is valid
{
x = scanner.nextInt();
y = scanner.nextInt();
} while(x > 99 || x < 0 || y > 99 || y < 0);
if (point[(y*10)+x].character != '+' || point[(y*10)+x] == point[prevx+(prevy*10)]) //check for invalid space space
{
System.out.printf("Invalid Space!");
if (player == 0) //Switch the player on an invalid space so it can be switched back immedialty
player = 1;
else
player = 0;
}
Board.placeStone(point,x,y,players[player]);
if (player == 0)
player = 1;
else
player = 0;
Board.checkBoard(point);
}
}
}
class Stone
{
char character = '+';
int x = 0, y = 0;
static int libs[][] = new int[4][2];
public static void initLibs(int xcord, int ycord)
{
for (int i = 0; i < 4; i++)
{
libs[i][0] = xcord;
libs[i][1] = ycord;
}
libs[0][0]--;
libs[1][1]--;
libs[2][0]++;
libs[3][1]++;
}
}
class Board
{
public static void print(Stone[] point, char player)
{
int line = 0;
//System.out.print("\u001b[2J");
System.out.print("\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");
System.out.printf("The turn belongs to player %c\n\n",player);
System.out.printf(" 0 1 2 3 4 5 6 7 8 9\n\n");
for(int i = 0; i < 10; i++)
{
line = i * 10;
System.out.printf("%d ",i);
for (int j = 0; j < 10; j++)
{
System.out.printf("%c",point[j+line].character);
if (j < 9)
System.out.printf("--");
else
System.out.printf("\n");
}
if (i < 9)
System.out.printf(" | | | | | | | | | |\n"); //print 9
}
}
public static void placeStone(Stone[] point, int x, int y, char player)
{
point[(y*10)+x].character = player;
}
public static void init(Stone[] point)
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
point[j+i*10].character = '+';
point[j+i*10].x = j;
point[j+i*10].y = i;
System.out.printf("(%d,%d)",i,j);
point[j+i*10].initLibs(j,i);
}
}
}
public static void checkBoard(Stone[] point)
{
int x, y, deps;
for (int j = 0; j < 100; j++)
{
deps = 0;
for (int i = 0; i < 4; i++)
{
x = point[j].libs[i][0]; // Collect x value of nth ordered pair array for the lib
y = point[j].libs[i][1]; // Collect y value
System.out.printf("(%d,%d)",x,y);
if (x < 0 || x > 9 || y < 0 || y > 9) // If the lib if off the board, it is the edge is is a netural dep
deps++;
else if(point[x+y*10].character != '+') //If the lib is an enemy stone, add a dep
deps++;
}
System.out.printf("______________________________");
System.out.printf("dep: %d\n",deps);
if (deps == 4) // If 4 dependencies are collected than that stone is captured
point[j].character = 'X';
}
}
}