Skip to content
Open
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
30 changes: 22 additions & 8 deletions src/snakegame/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
import java.util.Date;
import java.util.Scanner;

enum Key{
PLAYER1_UP('w'),
PLAYER1_DOWN('s'),
PLAYER1_LEFT('a'),
PLAYER1_RIGHT('d'),
PLAYER2_UP('o'),
PLAYER2_DOWN('l'),
PLAYER2_LEFT('k'),
PLAYER2_RIGHT(';');

private final char value;
Key(char value){this.value = value;}
}

public class Test {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
Expand All @@ -13,29 +27,29 @@ public static void main(String args[]) {
System.out.print("input: ");
char ch = sc.next().charAt(0);

if (ch == 'w') {
if (ch == Key.PLAYER1_UP.ordianl()) {
for (int i = 0; i < ctx.players.length; i++) if (ctx.players[i].getType() == PlayerType.PLAYER1) ctx.players[i].setDirection(DIRECTION.NORTH);
}
else if (ch == 's') {
else if (ch == Key.PLAYER1_DOWN.ordianl()) {
for (int i = 0; i < ctx.players.length; i++) if (ctx.players[i].getType() == PlayerType.PLAYER1) ctx.players[i].setDirection(DIRECTION.SOUTH);
}
else if (ch == 'd') {
else if (ch == Key.PLAYER1_RIGHT.ordianl()) {
for (int i = 0; i < ctx.players.length; i++) if (ctx.players[i].getType() == PlayerType.PLAYER1) ctx.players[i].setDirection(DIRECTION.EAST);
}
else if (ch == 'a') {
else if (ch == Key.PLAYER1_LEFT.ordianl()) {
for (int i = 0; i < ctx.players.length; i++) if (ctx.players[i].getType() == PlayerType.PLAYER1) ctx.players[i].setDirection(DIRECTION.WEST);
}

else if (ch == 'o') {
else if (ch == Key.PLAYER2_UP.ordianl()) {//UP
for (int i = 0; i < ctx.players.length; i++) if (ctx.players[i].getType() == PlayerType.PLAYER2) ctx.players[i].setDirection(DIRECTION.NORTH);
}
else if (ch == 'l') {
else if (ch == Key.PLAYER2_DOWN.ordianl()) {//DOWN
for (int i = 0; i < ctx.players.length; i++) if (ctx.players[i].getType() == PlayerType.PLAYER2) ctx.players[i].setDirection(DIRECTION.SOUTH);
}
else if (ch == ';') {
else if (ch == Key.PLAYER2_RIGHT.ordianl()) {//RIGHT
for (int i = 0; i < ctx.players.length; i++) if (ctx.players[i].getType() == PlayerType.PLAYER2) ctx.players[i].setDirection(DIRECTION.EAST);
}
else if (ch == 'k') {
else if (ch == Key.PLAYER2_LEFT.ordianl()) {//LEFT
for (int i = 0; i < ctx.players.length; i++) if (ctx.players[i].getType() == PlayerType.PLAYER2) ctx.players[i].setDirection(DIRECTION.WEST);
}

Expand Down