xxxxxxxxxx
import java.util.Arrays;
//Alex Rosenthal
//test implementation of 2 player snake
//March 22, 2015
boolean wrap = true; //snakes wrap to other edge of screen
ArrayList keyQ = new ArrayList<String>(); //List of pressed keys
ArrayList keyQ2 = new ArrayList<String>();
int direc = 0; // 0 = East, 1 = South, 2 = West, 3 = North
int direc2 = 2;
int len;
int len2;
int headY = 0;
int headX = 0;
int headY2 = 0;
int headX2 = 0;
int goalX;
int goalY;
boolean game;
Grid grid;
int c = 0;
int l = 0;
int s1 = 0;
int s2 = 0;
boolean s = true;
int bg = 75;
void setup(){
c = 0;
game = true;
size(500,500);
background(bg);
direc = 0;
direc2 = 2;
len = 11;
len2 = -11;
grid = new Grid(50);
grid.init();
headX = 0;
headY = grid.squares/2;
headX2 = grid.squares-1;
headY2 = headY+0;
s = true;
while(keyQ.size() != 0)
keyQ.remove(0);
while(keyQ2.size() != 0)
keyQ2.remove(0);
}
void draw(){
textAlign(CENTER,CENTER);
if(game){
if(c == 2){
checkKeyPressed();
c = 0;
if(direc == 0)
grid.step(headX+1,headY,1);
else if(direc == 1)
grid.step(headX,headY+1,1);
else if(direc == 2)
grid.step(headX-1,headY,1);
else if(direc == 3)
grid.step(headX,headY-1,1);
if(direc2 == 0)
grid.step(headX2+1,headY2,2);
else if(direc2 == 1)
grid.step(headX2,headY2+1,2);
else if(direc2 == 2)
grid.step(headX2-1,headY2,2);
else if(direc2 == 3)
grid.step(headX2,headY2-1,2);
background(bg);
fill(0);
rect(0,0,width,height);
grid.set(headX, headY, len);
grid.set(headX2, headY2,len2);
grid.subtract();
grid.display();
if(l == 3){
if(headY != headY2 && headX != headX2)
l = 1;
else if(abs(headX-headX2)>1 || abs(headY-headY2)>1)
l = 1;
}
}
else c++;
}
else{
textSize(height/20);
if(s){
s = false;
if(l == 1)
s2 += 1;
else if(l == 2)
s1 += 1;
}
if(l == 1){
fill(150,0,0);
text("RED WINS",width/2,height/2);
}
else if(l == 2){
fill(0,0,150);
text("BLUE WINS",width/2,height/2);
//text("LENGTH: "+(len-1), w/3, 4*h/7);
}
else{
fill(150);
text("TIE",width/2,height/2);
}
fill(0,0,150);
textAlign(LEFT,TOP);
text(s1,0,0);
fill(150,0,0);
textAlign(RIGHT, TOP);
text(s2,width,0);
textAlign(CENTER,CENTER);
}
}
public class Grid{ // Grid class, stores integers that represent snakes and goals
public int[][] x;
public int squares;
public int SQ = 5;
public int gx;
public int gy;
public Grid(int sq){
squares = sq;
SQ = width/sq;
int[][] x = new int[sq][sq];
}
public Grid(){
this(50);
}
public void init(){
x = new int[squares][squares];
for(int i = 0; i<x.length-1; i++){
for(int j = 0; j < x[i].length-1; j++){
x[i][j] = 0;
}
}
setGoal();
}
public void display(){
fill(255);
noStroke();
rect(gx*SQ, gy*SQ, SQ, SQ);
for(int i = 0; i<x.length; i++){
for(int j = 0; j < x[i].length; j++){
fill(0,0,255);
noStroke();
if(x[i][j] > 0){
rect(i*SQ, j*SQ, SQ, SQ);}
fill(255,0,0);
noStroke();
if(x[i][j] < 0){
rect(i*SQ, j*SQ, SQ, SQ);}
}
}
}
public void subtract(){
for(int i = 0; i<x.length; i++){
for(int j = 0; j < x[i].length; j++){
if(x[i][j] > 0){
x[i][j] -= 1;}
else if(x[i][j] <0){
x[i][j] += 1;}
}
}
}
public void set(int vx, int vy, int val){
x[vx][vy] = val;
}
public void setGoal(){
gx = floor(random(squares-1));
gy = floor(random(squares-1));
while(x[gx][gy]!=0){
gx = floor(random(squares));
gy = floor(random(squares));
}
}
public void step(int sx, int sy, int h){
if(!(sx>squares-1 | sx<0 | sy>squares-1 | sy<0)){
if(sx == gx && sy == gy){
if(h == 1)
len += 5;
else if(h == 2)
len2 -= 5;
grid.setGoal();
if(h == 1){
headX = sx;
headY = sy;
}
else if(h == 2){
headX2 = sx;
headY2 = sy;
}
}
else if(x[sx][sy] != 0){
l = h;
if((sx==headX && sy==headY)||(sx==headX2 && sy==headY2))
l = 3;
game = false;
}
else{
if(h == 1){
headX = sx;
headY = sy;
}
else if(h == 2){
headX2 = sx;
headY2 = sy;
}
}
}
else{
if(wrap){
if(sx<0)
step(squares - 1,sy,h);
else if(sy<0)
step(sx,squares - 1,h);
else if(sx>=squares)
step(0,sy,h);
else if(sy>=squares)
step(sx,0,h);
}
else{
l = h;
game = false;
}
}
}
}
void checkKeyPressed(){
if(keyQ.size() > 0){
if(keyQ.get(0).equals("w") && direc!=1)
direc = 3;
else if(keyQ.get(0).equals("d") && direc!=2)
direc = 0;
else if(keyQ.get(0).equals("s") && direc!=3)
direc = 1;
else if(keyQ.get(0).equals("a") && direc!=0)
direc = 2;
keyQ.remove(0);
}
if(keyQ2.size()>0){
if(keyQ2.get(0).equals("I") && direc2!=1)
direc2 = 3;
else if(keyQ2.get(0).equals("L") && direc2!=2)
direc2 = 0;
else if(keyQ2.get(0).equals("K") && direc2!=3)
direc2 = 1;
else if(keyQ2.get(0).equals("J") && direc2!=0)
direc2 = 2;
keyQ2.remove(0);
}
}
void keyPressed(){
if(key == 'r' || key == ' ')
setup();
else if(key!= CODED)
keyQ.add(str(key));
else{
if(keyCode==UP)
keyQ2.add("I");
else if(keyCode == LEFT)
keyQ2.add("J");
else if(keyCode == RIGHT)
keyQ2.add("L");
else if(keyCode == DOWN)
keyQ2.add("K");
}
}