Tron with classes and arrays

I have been stuck on this project for days and some help would be appreciated. My classmates along with me have to make Tron but with classes and arrays. Hope anybody could help. Thank you.

*****Light_Boxes_L*****

Game game = new Game();
int counter=0;

Player red;
Player blue;



void setup()
{
  size(601,601);
  rectMode(CORNER);
  PFont f = createFont("Times New Roman", 40);

}

void draw()
{
  if(counter==9)
  {
    game.update();
  }
  game.drawSelf();
  counter=(counter+1)%10;
}

void keyPressed()
{
  red = game.getRedPlayer();
  blue = game.getBluePlayer();

  // TO DO: Add code to handle user inputs

  if(key == 'w')
    red.setDirection(red.UP_VALUE);
  else if(key == 's')
    red.setDirection(red.DOWN_VALUE);
  else if(key == 'a')
    red.setDirection(red.LEFT_VALUE);
  else if(key == 'd')
    red.setDirection(red.RIGHT_VALUE);
}


*****Game*******

class Game
{


  final int EMPTY = 0;
  final int RED_PLAYER = 1;
  final int BLUE_PLAYER = 2;
  final int RED_TRAIL = 3;
  final int BLUE_TRAIL = 4;
  final int DEAD = 5;

  final int RED_WINS = 0;
  final int BLUE_WINS = 1;
  final int TIE = 2;
  final int PLAYING = 3;

  int[][] grid = new int[30][30];

  int status =PLAYING;

  Player pR = null;
  Player pB = null;



  //f = createFont("Times New Roman", 40);

  Game()
  {
    grid = new int[30][30];
    reset();
  }



  void reset()
  {
    // TO DO: Set status
    status=PLAYING;
    pR = new Player(4, 15, true);
    pB = new Player(25, 15, false);

    // TO DO: Add code to set every location in grid to EMPTY
    for (int r=0; r<30; r++)
      for (int c=0; c<30; c++)
        grid[r][c]=EMPTY;
  }


  void drawSelf()
  {

    grid[pR.getY()][pR.getX()]=RED_PLAYER;
    grid[pB.getY()][pB.getX()]=BLUE_PLAYER;
    // TO DO: Add code to draw all the squares in the game
    for (int r=0; r<30; r++)
    {
      for (int c=0; c<30; c++)
      {
        stroke(0);
        strokeWeight(1);
        if (grid[r][c]==EMPTY)
          fill(255);
        else if (grid[r][c]==RED_PLAYER)
          fill(255, 0, 0);
        else if (grid[r][c]==BLUE_PLAYER)
          fill(0, 0, 255);
        else if (grid[r][c]==RED_TRAIL)
          fill(127, 0, 0);
        else if (grid[r][c]==BLUE_TRAIL)
          fill(0, 0, 127);
        else if (grid[r][c]==DEAD)
          fill(0);

        rect(c*20, r*20, 20, 20);
      }
    }
    // TO DO: Add code to draw each player

    // TO DO: Add code to draw all the lines

    // TO DO: Add code to draw game results
  }

  public void update()
  {
    if (status == PLAYING)
    {

      int d = pR.getDirection();
      int c = pR.getX();
      int r = pR.getY();

      if (d == pR.UP_VALUE)
      {

        if (r-1 < 0 || grid[r-1][c] != EMPTY)
        {
          // TO DO: kill the red player
          grid[pR.getY()][pR.getX()]=DEAD;
          // TO DO: Put DEAD in the grid at the red players location
          fill(0);
          rect(pR.getY(), pR.getX(), 20, 20);
          noFill();
        } else
        {
          // TO DO: place a RED Trail in the gird

          fill(127, 0, 0);
          rect(pR.getY(), pR.getX(), 20, 20);
          noFill();
          grid[r][c] = grid[r-1][c];
          // TO DO: move the player
        }
      } else if (d == pR.DOWN_VALUE)
      {
        if (r+1>=30 || grid[r+1][c] != EMPTY)
        {
          // TO DO: kill the red player
          grid[pR.getY()][pR.getX()]=DEAD;
          // TO DO: Put DEAD in the grid at the red players lcoation
          fill(0);
          rect(pR.getY(), pR.getX(), 20, 20);
          noFill();
        } else
        {
          // TO DO: place a RED Trail in the gird
          fill(127, 0, 0);
          rect(pR.getY(), pR.getX(), 20, 20);
          noFill();
          // TO DO: move the player
        }
      } else if (d == pR.LEFT_VALUE)
      {
        if (c-1<0 || grid[r][c-1] != EMPTY)
        {
          // TO DO: kill the red player
          grid[pR.getY()][pR.getX()]=DEAD;
          // TO DO: Put DEAD in the grid at the red players lcoation
          fill(0);
          rect(pR.getY(), pR.getX(), 20, 20);
          noFill();
        } else
        {
          // TO DO: place a RED Trail in the gird
          fill(127, 0, 0);
          rect(pR.getY(), pR.getX(), 20, 20);
          noFill();
          // TO DO: move the player
        }
      } else if (d == pR.RIGHT_VALUE)
      {
        if (c+1>=30 || grid[r][c+1] != EMPTY)
        {
          // TO DO: kill the red player
          grid[pR.getY()][pR.getX()]=DEAD;
          // TO DO: Put DEAD in the grid at the red players lcoation
          fill(0);
          rect(pR.getY(), pR.getX(), 20, 20);
          noFill();
        } else
        {
          // TO DO: place a RED Trail in the gird
          fill(127, 0, 0);
          rect(pR.getY(), pR.getX(), 20, 20);
          noFill();
          // TO DO: move the player
        }
      }

      // move blue
      d = pB.getDirection();
      c = pB.getX();
      r = pB.getY();

      if (d == pB.UP_VALUE)
      {
        if (r-1<0 || grid[r-1][c] != EMPTY)
        {
          // TO DO: kill the blue player
          grid[pB.getY()][pB.getX()]=DEAD;
          // TO DO: Put DEAD in the grid at the blue players lcoation
          fill(0);
          rect(pB.getY(), pB.getX(), 20, 20);
          noFill();
        } else
        {
          // TO DO: place a Blue Trail in the gird
          fill(0, 0, 127);
          rect(pB.getY(), pB.getX(), 20, 20);
          noFill();
          grid[r][c] = grid[r+1][c];
          // TO DO: move the player
        }
      } else if (d == pB.DOWN_VALUE)
      {
        if (r+1>=30 || grid[r+1][c] != EMPTY)
        {
          // TO DO: kill the blue player
          grid[pB.getY()][pB.getX()]=DEAD;
          // TO DO: Put DEAD in the grid at the blue players lcoation
          fill(0);
          rect(pB.getY(), pB.getX(), 20, 20);
          noFill();
        } else
        {
          // TO DO: place a Blue Trail in the gird
          fill(0, 0, 127);
          rect(pB.getY(), pB.getX(), 20, 20);
          noFill();
          grid[r][c] = grid[r-1][c];
          // TO DO: move the player
        }
      } else if (d == pB.LEFT_VALUE)
      {
        if (c-1<0 || grid[r][c-1] != EMPTY)
        {
          // TO DO: kill the blue player
          grid[pB.getY()][pB.getX()]=DEAD;
          // TO DO: Put DEAD in the grid at the blue players lcoation
          fill(0);
          rect(pB.getY(), pB.getX(), 20, 20);
          noFill();
        } else
        {
          // TO DO: place a Blue Trail in the gird
          fill(0, 0, 127);
          rect(pB.getY(), pB.getX(), 20, 20);
          noFill();
          grid[r][c] = grid[r][c-1];
          // TO DO: move the player
        }
      } else if (d == pB.RIGHT_VALUE)
      {
        if (c+1>=30 || grid[r][c+1] != EMPTY)
        {
          // TO DO: kill the blue player
          grid[pB.getY()][pB.getX()]=DEAD;
          // TO DO: Put DEAD in the grid at the blue players lcoation
          fill(0);
          rect(pB.getY(), pB.getX(), 20, 20);
          noFill();
        } else
        {
          // TO DO: place a Blue Trail in the gird
          fill(0, 0, 127);
          rect(pB.getY(), pB.getX(), 20, 20);
          noFill();
          grid[r][c] = grid[r][c+1];
          // TO DO: move the player
        }
      }
    }

      if (grid[pR.getY()][pR.getX()] != EMPTY || (pR.getY()==pB.getY() && pR.getX() == pB.getX()))
        pR.setAlive(false);

      // TO DO: updates status if needed
    }

    Player getRedPlayer()
    {  
      return pR;
    }

    Player getBluePlayer()
    {  
      return pB;
    }

    int getStatus()
    {  
      return status;
    }
  }



****Player*****

class Player
{

  int[][] grid = new int[30][30];
  // TO DO: Add the attributes for Player
  int x;
  int y;
  int direction;
  boolean alive;
  boolean red;
  final int UP_VALUE=0;
  final int DOWN_VALUE=1;
  final int LEFT_VALUE=2;
  final int RIGHT_VALUE=3;


  Player(int startX, int startY, boolean startRed)
  {



    // To Do: Add code for method

    grid = new int[30][30];

    alive=true;
    this.red=startRed;
    this.x=startX;
    this.y=startY;


    if (red==true)
      direction=RIGHT_VALUE; 
    else
      direction=LEFT_VALUE;

    for(int c = 0; c < 30; c++)
      for(int r = 0; r < 30; r++)
      {

    if (direction == RIGHT_VALUE)
    {

      grid[r][c] = grid[r][c+1];

    }

      }
  }

  boolean getAlive()
  {  // To Do: Add code for method }
    return alive;
  }  
  int getX()
  {  // To Do: Add code for method }
    return x;
  }
  int getY()
  {  // To Do: Add code for method }   
    return y;
  }

  int getDirection()
  {  // To Do: Add code for method }
    return direction;
  }

  void setDirection(int newDirection)
  {  // To Do: Add code for method }
    this.direction=newDirection;
  }


  void setAlive(boolean newAlive)
  {  // To Do: Add code for method }
    this.alive=newAlive;
  }

  void setX(int newX)
  {  // To Do: Add code for method }
    this.x=newX;
  }

  void setY(int newY)
  {  // To Do: Add code for method }
    this.y=newY;
  }
}
Tagged:

Answers

  • Edit post, highlight code, press Ctrl-o to format

  • K did that

  • What doesn’t work?

    Line numbers?

  • Line 139 and 140, set two user variables to save you having to call getx and gety all the time...

    Lines 148, 151, 158, 168, 171... you use getx and gety anyway, making the code less readable.

    Also, shouldn't those local variables be called x and y rather than r and c?

  • (actually, I think you use r, c more often so change the player class to match if that's easier)

  • k sure il change the x, y to r,c but what do you mean by changing the player class?

  • i need movement to work cause it wont do that so yeah

  • Why does your Player class have a grid? Shouldn't just the Game class have the grid?

  • all the // todo comments make me think this is a template that has been supplied that needs padding out. which makes it hard to know who to blame for messy design and what to suggest as fixes.

Sign In or Register to comment.