Results 1 to 4 of 4

Thread: Tron AI

  1. #1
    talcorn is offline Member
    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Default Tron AI

    Hi, so I'm writing a Tron AI for my APCS class. Everything works fine for me, except that everytime I tell my AI to turn from moving a long the x-coordinate to moving along the y-coordinate, it jumps 1 unit, leaving a black square (as you can see in the picture). Can someone help me with this? Below are the codes.
    Tron AI-untitled.png

    The board (made by my teacher):
    Java Code:
    import javax.swing.*;
    
    import java.awt.Color;
    
    import java.awt.GridLayout;
    
    import java.awt.List;
    
    import java.util.ArrayList;
    
    
    
    public class TronBoard extends JFrame{
    
     
    
     ArrayList<JPanel> cells;
    
     JPanel arena;
    
     GridLayout layout;
    
     boolean win;
    
     LightCycleA P1;
    
     LightCycleB P2;
    
     
    
     public TronBoard(){
    
     win = false;
    
     cells = new ArrayList<>(6000);
    
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
     this.setSize(500, 300);
    
     layout = new GridLayout(60, 100);
    
     arena = new JPanel(layout);
    
     arena.setSize(500, 300);
    
     for (int i = 0; i < 60; i++) {
    
     for (int j = 0; j < 100; j++) {
    
     JPanel cellToAdd = new JPanel();
    
     cellToAdd.setBackground(Color.BLACK);
    
     cells.add(cellToAdd);
    
     arena.add(cellToAdd);
    
     }
    
     }
    
     this.add(arena);
    
     this.validate();
    
     this.pack();
    
     this.setVisible(true);
    
     }
    
     
    
     public void startLoc(){
    
     int x1 = (int) Math.floor(Math.random()*50);
    
     int y1 = (int) Math.floor(Math.random()*30);
    
     int x2 = ((int) Math.floor(Math.random()*50))+50;
    
     int y2 = ((int) Math.floor(Math.random()*30))+30;
    
     P1 = new LightCycleA (x1,y1, this);
    
     P2 = new LightCycleB (x2,y2, this);
    
     }
    
     
    
     public void rocks(){
    
     for(int i = 0; i<=((int) Math.floor(Math.random()*20)); i++){
    
     int rockX = ((int) Math.floor(Math.random()*97))+1;
    
     int rockY = ((int) Math.floor(Math.random()*57))+1;
    
     changeCell(rockX,rockY,Color.WHITE);
    
     changeCell(rockX+1,rockY,Color.WHITE);
    
     changeCell(rockX,rockY+1,Color.WHITE);
    
     changeCell(rockX+1,rockY+1,Color.WHITE);
    
     }
    
     }
    
     
    
     public void runGame(){
    
     int[] P1Step = P1.nextStep();
    
     int[] P2Step = P2.nextStep();
    
     int x1 = P1Step[0]; 
    
     int y1 = P1Step[1];
    
     int x2 = P2Step[0];
    
     int y2 = P2Step[1];
    
     if(!getCell(x1,y1)||!getCell(x2,y2))
    
     win = true;
    
     else if((x1==x2)&&(y1==y2))
    
     win = true;
    
     else{
    
     changeCell(x1,y1, Color.RED);
    
     changeCell(x2,y2, Color.CYAN);
    
     P1.newX(x1);
    
     P2.newX(x2);
    
     P1.newY(y1);
    
     P2.newY(y2);
    
     try{
    
     Thread.sleep(40L);
    
     }
    
     catch(Exception e){}
    
     }
    
     }
    
     
    
     public void changeCell(int x, int y, Color c){
    
     JPanel toChange = cells.get(x+y*100);
    
     toChange.setBackground(c);
    
     this.repaint();
    
     }
    
     
    
     public boolean getCell(int x, int y){
    
     if((x<0)||(y<0)||(x>99)||(y>59))
    
     return false;
    
     else
    
     return cells.get(x+y*100).getBackground().equals(Color.BLACK);
    
     }
    
    
    
    }
    The main game (made by my teacher):
    Java Code:
    import javax.swing.JFrame;
    
    import javax.swing.JOptionPane;
    
    
    
    public class Tron {
    
     public static void main(String[] args) {
    
     TronBoard theGrid = new TronBoard();
    
     theGrid.rocks();
    
     theGrid.startLoc();
    
     while(!theGrid.win){
    
     theGrid.runGame();
    
     }
    
     JFrame popup = new JFrame();
    
     JOptionPane.showMessageDialog(popup,
    
         "The game is over!!!");
    
     }
    
    }
    My AI:
    Java Code:
    public class LightCycleA {
    
    	int xPos;
    	int yPos;
    	TronBoard Tron;
    	int count = 0;
    	
    	public LightCycleA(int x1, int y1, TronBoard tronBoard) {
    		
    		xPos = x1;
    		yPos = y1;
    		Tron = tronBoard;
    	}
    	 
    	public int[] nextStep() {
    		int[] next = {xPos-1, yPos};
    		////////////////////////
    		if (xPos<=5){
    			next[0] = xPos;
    			next[1] = yPos-1;
    		}
    		////////////////////////
    		if (yPos==1){
    			next[0]=xPos-1;
    			next[1]=yPos;
    		}
    		////////////////////////
    		if (xPos==0){
    			next[0]=xPos;
    			next[1]=yPos+1;
    		}
    		///////////////////////
    		if (yPos==58){
    			next[0]=xPos+1;
    			next[1]=yPos;
    		}
    		///////////////////////
    		if (xPos>=5&&yPos==58){
    			next[0] = xPos;
    			next[1] = yPos-1;
    		}
    		///////////////////////
    		if (yPos==30&&xPos>=5){
    			next[0] = xPos+1;
    			next[1] = yPos;
    		}
    		
    		return next;
    	}
    
    	public void newX(int x1) {
    		xPos = nextStep()[0];
    	}
    
    	public void newY(int y1) {
    		yPos = nextStep()[1];		
    	}
    }

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Tron AI

    Without even looking at the code... could it have something to do with the fact that the lightcycle sprites are 2x2 pixels and they draw a line only 1 pixel wide? What coordinates within the lightcycle sprite represent its position, and do those coordinates correspond to where the pixels of the trail are drawn?
    Get in the habit of using standard Java naming conventions!

  3. #3
    talcorn is offline Member
    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Default Re: Tron AI

    the sprites are 1x1, the white blocks are 2x2, the top left corner is (0,0) and the bottom right is (99,59). The game uses the standard coordinate plane.

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Tron AI

    What I'm getting at is, what pixel of the white blocks do the lines "come out of"? And could that affect the gaps you're seeing when you move in certain directions?
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. TRON source code?
    By Army in forum New To Java
    Replies: 10
    Last Post: 02-22-2012, 10:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •