Skip to content
This repository was archived by the owner on Apr 23, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
properties.xml
*.DS_Store
/store/
.project
37 changes: 37 additions & 0 deletions src/tak/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public class Client extends Thread {

String resignString = "^Game#(\\d+) Resign";
Pattern resignPattern;

String markString = "^Game#(\\d+) (Mark|Unmark) ([A-Ha-h][1-8])";
Pattern markPattern;

String seekString = "^Seek (\\d) (\\d+)";
Pattern seekPattern;
Expand Down Expand Up @@ -105,6 +108,9 @@ public class Client extends Thread {
String shoutString = "^Shout ([^\n\r]{1,256})";
Pattern shoutPattern;

String ingameString = "^Game#(\\d+) Chat ([^\n\r]{1,256})";
Pattern ingamePattern;

String pingString = "^PING$";
Pattern pingPattern;

Expand Down Expand Up @@ -157,6 +163,7 @@ public class Client extends Thread {
drawPattern = Pattern.compile(drawString);
removeDrawPattern = Pattern.compile(removeDrawString);
resignPattern = Pattern.compile(resignString);
markPattern = Pattern.compile(markString);
wrongRegisterPattern = Pattern.compile(wrongRegisterString);
seekPattern = Pattern.compile(seekString);
acceptSeekPattern = Pattern.compile(acceptSeekString);
Expand All @@ -167,6 +174,7 @@ public class Client extends Thread {
unobservePattern = Pattern.compile(unobserveString);
getSqStatePattern = Pattern.compile(getSqStateString);
shoutPattern = Pattern.compile(shoutString);
ingamePattern = Pattern.compile(ingameString);
pingPattern = Pattern.compile(pingString);
loginGuestPattern = Pattern.compile(loginGuestString);

Expand Down Expand Up @@ -488,6 +496,35 @@ else if (game!=null && (m = resignPattern.matcher(temp)).find() && game.no == In
other.game = null;
}
}
//Mark/Unmark target board field.
else if (game != null && (m=markPattern.matcher(temp)).find() && game.no == Integer.parseInt(m.group(1))) {
String function = m.group(2);
String field = m.group(3).toLowerCase();
if (game.setMarked(function.equals("Mark"), field, player))
sendOK();
else
sendNOK();
}
//Relay ingame chat.
else if (game != null && (m=ingamePattern.matcher(temp)).find() && game.no == Integer.parseInt(m.group(1))) {
String msg = "<" + player.getName() + "> " + m.group(2);
if (game.white == player || game.black == player) {
msg = " PlayerChat " + msg;
if(!player.isGagged()) {
game.sendToOtherPlayer(game.white, "Game#" + game.no + msg);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ID of the game is not per se required to display a message, but it might come the time, when different games have different ingame chat windows. For that eventuality, the ID is transmitted.

game.sendToOtherPlayer(game.black, "Game#" + game.no + msg);
game.sendToSpectators("Game#" + game.no + msg);
} else//send to only gagged player
sendWithoutLogging("Game#" + game.no + msg);
}
else {
msg = " ObserverChat " + msg;
if(!player.isGagged()) {
game.sendToSpectators("Game#" + game.no + msg);
} else//send to only gagged player
sendWithoutLogging("Game#" + game.no + msg);
}
}
//Show game state
else if (game != null && (m=gamePattern.matcher(temp)).find() && game.no == Integer.parseInt(m.group(1))) {
sendOK();
Expand Down
62 changes: 59 additions & 3 deletions src/tak/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public enum gameS {WHITE_ROAD, BLACK_ROAD, WHITE_TILE, BLACK_TILE, DRAW,

static Map<Integer, Game> games=Collections.synchronizedMap(new HashMap<Integer, Game>());
static Set<Client> gameListeners = Collections.synchronizedSet(new HashSet<Client>());

class Board {
int boardSize;
int moveCount;
Expand All @@ -64,11 +64,13 @@ class Board {

int whiteTilesCount;
int blackTilesCount;

class Square {
private int file, row;
private ArrayList<Character> stack;
int graphNo;
boolean markedByPlayer = false;
boolean markedByObserver = false;

Square(int f, int r) {
stack = new ArrayList<>();
Expand Down Expand Up @@ -120,6 +122,22 @@ public String toString() {
public String stackString() {
return stack.toString();
}
public boolean isMarkedByPlayer()
{
return this.markedByPlayer;
}
public void setMarkedByPlayer(boolean marked)
{
this.markedByPlayer = marked;
}
public boolean isMarkedByObserver()
{
return this.markedByObserver;
}
public void setMarkedByObserver(boolean marked)
{
this.markedByObserver = marked;
}
}
Square[][] squares;

Expand Down Expand Up @@ -268,6 +286,7 @@ static void removeGame(Game g) {
void newSpectator(Client c) {
c.send("Observe "+shortDesc());
sendMoveListTo(c);
sendMarkedListTo(c);
spectators.add(c);
updateTime(c);
}
Expand All @@ -279,7 +298,31 @@ void resign(Player p) {
gameState = gameS.WHITE;
whenGameEnd();
}


boolean setMarked(boolean marked, String field, Player player) {
int file = field.charAt(0) - 'a';
int rank = field.charAt(1) - 1;
if (file < 0 || file >= this.board.boardSize
|| rank < 0 || rank >= this.board.boardSize)
return false;

String msg;
if (player == this.white || player == this.black) {
msg = "Game#" + no
+ (marked ? " Mark_Player " : " Unmark_Player ") + field;
this.board.squares[rank][file].setMarkedByPlayer(marked);
sendToOtherPlayer(white, msg);
sendToOtherPlayer(black, msg);
}
else {
msg = "Game#" + no
+ (marked ? " Mark_Observer " : " Unmark_Observer ") + field;
this.board.squares[rank][file].setMarkedByObserver(marked);
}
sendToSpectators(msg);
return true;
}

void saveBoardPosition() {
boardHistory.push(board.clone());
}
Expand Down Expand Up @@ -353,6 +396,19 @@ void sendMoveListTo(Client c) {
c.sendWithoutLogging("Game#"+no+" "+move);
}

void sendMarkedListTo(Client c) {
for(int i = 0; i < this.board.boardSize; ++i)
for (int j = 0; j < this.board.boardSize; ++j)
{
if (this.board.squares[i][j].isMarkedByPlayer())
c.sendWithoutLogging("Game#" + this.no + " Mark_Player "
+ ((char) (j + 'a')) + (i + 1));
if (this.board.squares[i][j].isMarkedByObserver())
c.sendWithoutLogging("Game#" + this.no + " Mark_Observer "
+ ((char) (j + 'a')) + (i + 1));
}
}

String shortDesc(){
StringBuilder sb=new StringBuilder("Game#"+no+" ");
sb.append(white.getName()).append(" vs ").append(black.getName());
Expand Down