Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e1a709acce06fc5d842321936f389b256d561c71 (plain) (blame)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*******************************************************************************
 * Copyright (c) 2000, 2016 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.examples.browser.demos.views;

import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.examples.browser.demos.BrowserDemoPlugin;
import org.eclipse.swt.examples.browser.demos.Pawns;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.TabItem;

public class PawnTab {

	Action pawnAction;
	Action editAction;
	
	Browser browser = null;
	ReversiListener reversiListener;

	final static int TYPE_WELCOME = 1;
	final static int TYPE_START = 2;
	final static int TYPE_BOARD = 3;
	final static int TYPE_BOARD_OVER = 4;

	final static String URL = "http://www.org.eclipse.swt.examples.browser.demos.pawns/";
	static String PLUGIN_PATH = BrowserDemoPlugin.PLUGIN_PATH;
	static String CSS_FOLDER = "css2";
	static String URL_CSS = PLUGIN_PATH+CSS_FOLDER+"/style.css";
	static String URL_WELCOME =PLUGIN_PATH+CSS_FOLDER+"/welcome.html";

	final static String ACTION_START_2_PLAYERS = "actionstart2players";
	final static String ACTION_START_1_PLAYER = "actionstart1player";
	final static String ACTION_WHITE = "actionwhite";
	final static String ACTION_BLACK = "actionblack";
	final static String ACTION_THEME = "actiontheme";

	static byte[][] game = null;
	static boolean isWhite = true;
	static int cntWhite = 0, cntBlack = 0;
	static boolean computer = false;
	static int cx, cy;
	final static byte EMPTY = 0;
	final static byte WHITE = 1;
	final static byte BLACK = 2;
	final static byte WALL = 3;
	
	static Pawns ttr = null;
	static int[] move = new int[2];
	
	public PawnTab(TabItem item) {		
		try {
			browser = new Browser(item.getParent(), SWT.NONE);
		} catch (SWTError e) {
			e.printStackTrace();
			return;
		}
		item.setText("Pawns");
		item.setControl(browser);

		Menu menu = new Menu(browser.getShell(), SWT.POP_UP);
		MenuItem item2 = new MenuItem(menu, SWT.PUSH);
		item2.setText("End Game");
		item2.addListener(SWT.Selection, e -> {
			game = null;
			isWhite = true;
			cntWhite = 0; cntBlack = 0;
			ttr = null;
			browser.setUrl(URL_WELCOME);
		});
		browser.setMenu(menu);
		
		reversiListener = new ReversiListener();
		browser.addLocationListener(reversiListener);
		browser.setUrl(URL_WELCOME);
	}

	static String getHtml(int type) {
		String html = null;
		switch (type) {
		case TYPE_BOARD:
		case TYPE_BOARD_OVER: {
			html = "<html><header><link rel=\"stylesheet\" type=\"text/css\" href=\""+URL_CSS+"\"></header><body><div class=\"board\"><table><tbody>";
			String classPlayerWhite = "playerwhite", classPlayerBlack = "playerblack";
			if (type == TYPE_BOARD_OVER) {
				if (cntWhite > cntBlack) {
					classPlayerWhite += " winner";
					classPlayerBlack += " loser";
				} else {
					classPlayerWhite += " loser";
					classPlayerBlack += " winner";
				}
			}
			String white = "<td class=\"white\"/>";
			String black = "<td class=\"black\"/>";
			String wall = "<td class=\"wall\"/>";
			for (int i = 0; i < game.length; i++) {
				html += "<tr>";
				for (int j = 0; j < game[0].length; j++) {
					switch (game[i][j]) {
						case EMPTY: {
							String empty = "<td class=\""+(isWhite ? "whitelink" : "blacklink")+"\">"+ (isWhite || (!isWhite && !computer) ? "<a href=\""+URL+(isWhite ? ACTION_WHITE : ACTION_BLACK)+"/xx"+i+"yy"+j+"\" class=\"empty\"/>" : "<div class=\"empty\"/>")+"</td>";
							html += empty; break;
						}
						case WHITE: html += white; break;
						case BLACK: html += black; break;
						case WALL: html += wall; break;
					}
				}
				html +="</tr>";
			}
			html += "</tbody></table></div>";
			html += "<div class=\""+classPlayerWhite+"\">"+cntWhite+"</div>";
			html += "<div class=\""+classPlayerBlack+"\">"+cntBlack+"</div>";
			html += "</body></html>";
			break;
		}
		}
		return html;
	}

	public class ReversiListener implements LocationListener {
		@Override
		public void changed(LocationEvent e) {
		}
		@Override
		public void changing(LocationEvent e) {
			try {
			final Browser browser = (Browser)e.widget;
			if (e.location.indexOf(ACTION_START_1_PLAYER) != -1 || e.location.indexOf(ACTION_START_2_PLAYERS) != -1) {
				computer = e.location.indexOf(ACTION_START_1_PLAYER) != -1;
				game = new byte[8][8];
				if (computer) ttr = new Pawns();
				for (int i = 0; i < 5; i++) game[(int)(Math.random()*game.length)][(int)(Math.random()*game[0].length)] = WALL;
				e.display.asyncExec(() -> browser.setText(getHtml(TYPE_BOARD)));
				e.doit = false;
				return;
			}
			if (e.location.indexOf(ACTION_THEME) != -1) {
				int index = e.location.indexOf(ACTION_THEME) + ACTION_THEME.length() + 1;
				CSS_FOLDER = e.location.substring(index, index + 4);
				URL_CSS = PLUGIN_PATH+CSS_FOLDER+"/style.css";
				URL_WELCOME = PLUGIN_PATH+CSS_FOLDER+"/welcome.html";
				e.display.asyncExec(() -> browser.setUrl(URL_WELCOME));
				e.doit = false;
				return;
			}
			byte player = EMPTY;
			if (e.location.indexOf(ACTION_WHITE) != -1) player = WHITE;
			else if (e.location.indexOf(ACTION_BLACK) != -1) player = BLACK;
			if (player != EMPTY) {
				int index = e.location.indexOf("xx") + 2;
				int x = Integer.parseInt(e.location.substring(index, index + 1));
				index = e.location.indexOf("yy") + 2;
				int y = Integer.parseInt(e.location.substring(index, index + 1));
				boolean hasMore = add(x, y, player);
				isWhite = player != WHITE;
				browser.setText(getHtml(hasMore ? TYPE_BOARD : TYPE_BOARD_OVER));
				if (computer && hasMore && !isWhite) play(e.display, browser, 5000);
				e.doit = false;
			}
			}catch (Exception e1 ) {
				e1.printStackTrace();
			}
		}
	}

	public static boolean add(int x, int y, byte color) {
		game[x][y] = color;
		int cnt = Math.min(x, y), n = 0;
		int other_color = color == WHITE ? BLACK : WHITE;
		for (int d = 1; d <= cnt; d++) {
			if (game[x-d][y-d] == other_color) n++;
			else if (game[x-d][y-d] != color) break;
			else { if (n > 0) for (d = 1; d <= n; d++) game[x-d][y-d] = color; break; }
		}
		cnt = Math.min(game.length - 1 - x, game[0].length - 1 - y); n = 0;
		for (int d = 1; d <= cnt; d++) {
			if (game[x+d][y+d] == other_color) n++;
			else if (game[x+d][y+d] != color) break;
			else { if (n > 0) for (d = 1; d <= n; d++) game[x+d][y+d] = color; break; }
		}
		cnt = Math.min(game.length - 1 - x, y); n = 0;
		for (int d = 1; d <= cnt; d++) {
			if (game[x+d][y-d] == other_color) n++;
			else if (game[x+d][y-d] != color) break;
			else { if (n > 0) for (d = 1; d <= n; d++) game[x+d][y-d] = color; break; }
		}
		cnt = Math.min(x, game[0].length - 1 - y); n = 0;
		for (int d = 1; d <= cnt; d++) {
			if (game[x-d][y+d] == other_color) n++;
			else if (game[x-d][y+d] != color) break;
			else { if (n > 0) for (d = 1; d <= n; d++) game[x-d][y+d] = color; break; }
		}
		cnt = y; n = 0;
		for (int d = 1; d <= cnt; d++) {
			if (game[x][y-d] == other_color) n++;
			else if (game[x][y-d] != color) break;
			else { if (n > 0) for (d = 1; d <= n; d++) game[x][y-d] = color; break; }
		}
		cnt = game[0].length - 1 - y; n = 0;
		for (int d = 1; d <= cnt; d++) {
			if (game[x][y+d] == other_color) n++;
			else if (game[x][y+d] != color) break;
			else { if (n > 0) for (d = 1; d <= n; d++) game[x][y+d] = color; break; }
		}
		cnt = x; n = 0;
		for (int d = 1; d <= cnt; d++) {
			if (game[x-d][y] == other_color) n++;
			else if (game[x-d][y] != color) break;
			else { if (n > 0) for (d = 1; d <= n; d++) game[x-d][y] = color; break; }
		}
		cnt = game.length - 1 - x; n = 0;
		for (int d = 1; d <= cnt; d++) {
			if (game[x+d][y] == other_color) n++;
			else if (game[x+d][y] != color) break;
			else { if (n > 0) for (d = 1; d <= n; d++) game[x+d][y] = color; break; }
		}

		boolean hasMore = false;
		cntWhite = 0; cntBlack = 0;
		for (int i = 0; i < game.length; i++)
			for (int j = 0; j < game[0].length; j++) {
				switch (game[i][j]) {
					case EMPTY: hasMore = true; break;
					case WHITE: cntWhite++; break;
					case BLACK: cntBlack++; break;
				}
			}
		return hasMore;
	}
	
	public static void play(final Display display, final Browser browser, int delay) {
		ttr.playRequest(game, BLACK);
		display.timerExec(3000, () -> {
			ttr.getBestMove(move);
			boolean hasMore = add(move[0], move[1], BLACK);
			isWhite = true;
			browser.setText(getHtml(hasMore ? TYPE_BOARD : TYPE_BOARD_OVER));
		});
	}
	
	public static void main(String[] args) {
	}
}

Back to the top