Oppure

Loading
17/01/13 0:12
sheva7
Ho trovato questo codice su internet ma non riesco a capire come fixarlo...
Nel caso vi fosse di aiuto è una sorta di snake multiplayer in cui non si devono mangiare le mele ma vince chi non si scontra contro il serpente avversario o contro il muro.
Comunque sia, mi hanno detto che il codice è funzionante ma non sono riuscito a sistemarlo.
Dopo varie modifiche sono riuscito solo a far aumentare il numero degli errori per cui, vi incollo la versione base dei file che ho trovato.

grazie anticipatamente a tutti.

Allora, il codice è questo solo che non riesco a capire come fixarlo...

FILE arena.java
/**
 * Arena class
 *
 * originally written by Elizabeth Sklar and Pablo Funes, summer 1998
 *
 * modified for teaching spring-2006/sklar
 *
 */


import java.awt.*;
import java.lang.*;
import java.applet.*;
import java.util.Vector;
import java.net.*;
import java.io.*;


public class Arena extends Canvas implements Runnable {
    
    Dimension grayDimension;
    Image     grayImage;
    Graphics  grayGraphics;
    
    public Tron              tron;
    
    private Thread           conductor;
    public  Player           player1;
    public  Player           player2;
    public  GPPlayer         gpplayer;
    public  NNPlayer         nnplayer;
    public  MyPlayer         myplayer;
    public  boolean          board[][];
    public  boolean          clear;
    public  boolean          startAgain = false;
    private int              xmax, ymax;
    
    public static final int  WAITING = 0;
    public static final int  RUNNING = 1;
    public static final int  RESTARTING = 2;
    public int               state;
    
    public int               lastmove;
    public int               gen_no;
    
    
    
    /**
     * Arena constructor
     *
     */
    public Arena( Tron t ) {
	setBackground( Color.black );
	player1 = null;
	player2 = null;
	conductor = null;
	board = null;
	state = WAITING;
	tron = t;
	gen_no = 0;
    } /* end of Arena constructor */


    
    /**
     * start()
     *
     * this method is called once, when the arena is initialized and
     * its runnable thread starts
     *
     */
    public void start() {
	xmax = size().width;
	ymax = size().height;
	if ( board == null ) {
	    board = new boolean[xmax][ymax];
	}
	gpplayer = new GPPlayer( "gp",Color.pink,this,xmax,ymax,(byte)1,null );
	nnplayer = new NNPlayer( "nn",Color.pink,this,xmax,ymax,(byte)1,null );
	myplayer = new MyPlayer( "my",Color.pink,this,xmax,ymax,(byte)1 );
	player2 = new HumanPlayer( "human",Color.cyan,this,xmax,ymax,(byte)2 );
	if ( grayImage != null ) {
	    this.getGraphics().drawImage( grayImage,0,0,this ); 
	}
	if ( conductor == null ) {
	    conductor = new Thread( this,"Arena" );
	    conductor.start();
	}
	else {
	    conductor.resume();
	}
    } /* end of start() */


    
    /**
     * stop()
     *
     * this method is called when the runnable thread stops
     *
     */
    public void stop() {
	conductor.suspend();
    } /* end of stop() */

    
    
    /**
     * selectPlayer1()
     *
     * this method is called when the user clicks on a button in the
     * interface and selects a robot controller
     *
     */
    public void selectPlayer1( 
			      int player,
			      String filename
			      ) {
	if ( player == Tron.GP ) {
	    gpplayer.getStrategy( filename );
	    player1 = gpplayer;
	    player1.name = "GP";
	}
	else if ( player == Tron.NN ) {
	    nnplayer.readNetwork( filename );
	    player1 = nnplayer;
	    player1.name = "NN";
	}
	else if ( player == Tron.MY ) {
	    player1 = myplayer;
	    player1.name = "MY";
	}
	player1.crash = false;
	player2.crash = false;
	clear = true;
	repaint();
    } /* end of selectPlayer1() */
    

    
    /**
     * startPlayers()
     *
     * this method is called when the user clicks on the "start"
     * button in the interface
     *
     */
    public void startPlayers() {
	clearBoard();
	player1.go( xmax/4,ymax/2 );
	player2.go( 3*xmax/4,ymax/2 );
	player1.score = tron.robotScore;
	state = RUNNING;
	lastmove = 0;
    } /* end of startPlayers() */
    

    
    /**
     * run()
     *
     * this is the "run" method that executes forever while the
     * runnable thread is executing
     *
     */
    public void run() {
	while ( true ) {
	    switch ( state ) {
	    case RUNNING:
		player1.step();
		player2.step();
		break;
	    case RESTARTING:
		if ( player1.crash && player2.crash ) {
		    // tie
		}
		else if ( player1.crash ) {
		    // player1 crashes -> player2 wins -> result="L"
		    player2.tallyWin();
		}
		else if ( player2.crash ) {
		    // player2 crashes -> player1 wins -> result="W"
		    player1.tallyWin();
		}
		player1.restart( player2.crash );
		player2.restart( player1.crash );
		state = WAITING;
		tron.updateScore();
		break;
	    case WAITING:
		if ( startAgain ) {
		    startAgain = false;
		    start();
		    startPlayers();
		}
		break;
	    }
	    repaint();
	    try { 
		Thread.sleep( 10 );
	    }
	    catch ( InterruptedException e ) {
	    }
	    if ( player1 != null ) { // hack hack
		player1.newPos();
		player2.newPos();
	    }
	}
    } /* end of run() */
    
    

    /**
     * update()
     *
     * this method is called by the native paint() method to draw on
     * this canvas
     *
     */
    public void update( Graphics g ) {
	if ( clear ) {
	    g.clearRect( 0,0,size().width,size().height );
	    clear = false;
	}
	if ( player1 != null ) {
	    player1.paint( g ); 
	}
	if ( player2 != null ) {
	    player2.paint( g );
	}
    } /* end of update() */

    
    
    /**
     * clearBoard()
     *
     * this method is called after a game ends to clear out the board
     *
     */
    public void clearBoard() {
	int i, j;
	for ( i=0; i<xmax; i++ )
	    for ( j=0; j<ymax; j++ )
		board[i][j] = false;
	clear = true;
    } /* end of clearBoard() */



} /* end of Arena class */



file HumanPlayer.java
import java.awt.*;
import java.lang.*;
import java.applet.*;

public class HumanPlayer extends Player {


    public HumanPlayer( String n, Color c, Arena a, int x, int y, byte number ) {
	name = n;
	color = c;
	arena = a;
	x_max = x;
	y_max = y;
	score = 0;
	player_no = number;

    } // end of HumanPlayer constructor
    

    // human player's d is controlled by interrupt-driven keyboard
    // handler; doesn't have to implement whereDoIGo
    
    
    public void go( int x,int y ) {
	super.go(x,y); // do everything the original GO does, plus custom inits
	didsomething = false;
    } // end of go()



} /* end of HumanPlayer class */



file MyPlayer.java
import java.awt.*;
import java.lang.*;
import java.applet.*;
import java.util.Random;



public class MyPlayer extends Player {


    public boolean didsomething = false;
    public Random random;


    /**
     * MyPlayer constructor
     *
     * you probably don't need to modify this method!
     *
     */
    public MyPlayer( String n, Color c, Arena a, int x, int y, byte number ) {
	name  = n;
	color = c;
	arena = a;
	x_max = x;
	y_max = y;
	player_no = number;
	random = new Random();
    } /* end of MyPlayer constructor */



    /**
     * whereDoIGo
     *
     * this is the method you need to modify.
     * you need to decide where your robot should go at each time step
     * (this method is called each time the robot can make a move).
     *
     * there are constants defined in Tron.java for each of the four
     * possible directions that the robot can go:
     *  public static final int NORTH = 2;
     *  public static final int EAST  = 1;
     *  public static final int SOUTH = 0;
     *  public static final int WEST  = 3;
     *
     * this method has to return one of those values.
     * it is up to you to decide, based on the state of the board and
     * everything you have learned about AI, which of these four values to
     * return each time this method is called.
     *
     */
    public int whereDoIGo() {
	// move randomly
	//return( Math.abs( random.nextInt() % 4 ));
	// "tit for tat" player (copy human)
	return( arena.player2.d );

    } /* end of whereDoIGo() */

    
} /* end of MYPlayer class */


file Player.java
import java.awt.*;
import java.lang.*;
import java.applet.*;



public class Player {

    public String       name;
    public Color        color;
    public Arena        arena;
    public byte         player_no;

    public boolean didsomething  = false;

    public int          x_max;
    public int          y_max;
    public static final int NORTH = 2;
    public static final int EAST  = 1;
    public static final int SOUTH = 0;
    public static final int WEST  = 3;

    public static final int CRASH_DELTA = 10;

    // previous position in each dimension
    public int          x0, y0;
    // current position in each dimension
    public int          x1, y1;
    // direction of movement
    public int          d;
    // last direction of movement
    public int          old_d;
    public boolean      crash;
    public int          score;



    /**
     * Player constructor
     *
     */
    public Player() {
    } // end of default Player constructor



   
    public void start() {
	// default start method is empty
    } // end of start()



    
    public void stop()  {
	// default stop method is empty 
    } // end of stop()



    
    public void restart( boolean theOtherGuyCrashed ) {
	// default restart method is empty
    } // end of restart()



   
    public int whereDoIGo() {
	// default player is constant
	return d;
    } // end of whereDoIGo()



    
    public void go( int x,int y ) {
	x0 = x;
	y0 = y;
	x1 = x0;
	y1 = y0;
	old_d = d = SOUTH;
	crash = false;
	arena.board[x0][y0] = true;
    } 



  
    public void step() {
	if (( d = whereDoIGo()) != old_d ) { 
	    old_d = d;
	}
	crash = markBoard( d );
	if ( crash )
	    arena.state = arena.RESTARTING;
    } 



   
    public void paint( Graphics g ) {
	if ( crash ) {
	    g.setColor( Color.red );
	    g.drawLine( x1-CRASH_DELTA,y1-CRASH_DELTA,
			x1+CRASH_DELTA,y1+CRASH_DELTA );
	    g.drawLine( x1,y1-CRASH_DELTA,x1,y1+CRASH_DELTA );
	    g.drawLine( x1+CRASH_DELTA,y1-CRASH_DELTA,
			x1-CRASH_DELTA,y1+CRASH_DELTA );
	    g.drawLine( x1-CRASH_DELTA,y1,x1+CRASH_DELTA,y1 );
	}
	else {
	    g.setColor( color );
	    g.drawLine( x0,y0,x1,y1 );
	}
    } 


    
    
    public void newPos() {
	x0 = x1;
	y0 = y1;
	paint( arena.getGraphics() );
    }   


    
    
    public boolean markBoard( int direction ) {
	boolean r = false;
	int i;
	switch ( direction ) {
	case SOUTH:
	    y1++;
	    if ( y1 >= y_max ) {
		y1 = 0;
		y0 = y1;
	    }
	    if ( r = arena.board[x1][y1] ) {
		break;
	    }
	    arena.board[x1][y1] = true;
	    break;
	case NORTH:
	    y1--;
	    if ( y1 < 0 ) {
		y1 = y_max - 1;
		y0 = y1;
	    }
	    if ( r = arena.board[x1][y1] ) {
		break;
	    }
	    arena.board[x1][y1] = true;
	    break;
	case EAST:
	    x1++;
	    if ( x1 >= x_max ) {
		x1 = 0;
		x0 = x1;
	    }
	    if ( r = arena.board[x1][y1] ) {
		break;
	    }
	    arena.board[x1][y1] = true;
	    break;
	case WEST:
	    x1--;
	    if ( x1 < 0 ) {
		x1 = x_max - 1;
		x0 = x1;
	    }
	    if ( r = arena.board[x1][y1] ) {
		break;
	    }
	    arena.board[x1][y1] = true;
	    break;
	default:
	    System.out.println( "UH-OH!" );
	    break;
	}
	return( r );
    } 


   
    public void tallyWin() {
	score++;
    } 



} /* end of Player class */



file Tron.java
import java.awt.*;
import java.lang.*;
import java.applet.*;
import java.util.*;


public class Tron extends Frame {
    
    public static final int NORTH = 2;
    public static final int EAST  = 1;
    public static final int SOUTH = 0;
    public static final int WEST  = 3;
    
    public String idParam;
    public Arena  arena;
    public Label  statusLabel;
    public Button startButton;
    public Button quitButton;
    public Button pickGPButton;
    public Button pickNNButton;
    public Button pickMyButton;
    public static Random random;

    public static String  gpfile = "gp.2220000";
    public static String  nnfile = "nn.700";
    
    public static final int NONE  = -1;
    public static final int HUMAN = 0;
    public static final int GP    = 1;
    public static final int NN    = 2;
    public static final int MY    = 3;
    public static int player1;
    public static int player2;

    
    public int robotScore, humanScore;



    /**
     * main()
     *
     */
    public static void main( String args[] ) { 
	
	Tron tron = new Tron();
	tron.setTitle( "Tron" );
	
	tron.player1 = NONE;
	tron.player2 = HUMAN;

	tron.robotScore = 0;
	tron.humanScore = 0;
	
	tron.arena = new Arena( tron );
	tron.arena.resize( 256,256 );

	GridBagLayout layout = new GridBagLayout();
	GridBagConstraints c = new GridBagConstraints();
	
	tron.setLayout( layout );
	
	c.gridx = 0;
	c.gridy = 0;
	c.gridwidth = 4;
	layout.setConstraints( tron.arena,c );
	tron.add( tron.arena );
	
	c.gridwidth = 1;
	c.weightx   = 1;
	c.anchor    = GridBagConstraints.CENTER;

	tron.pickGPButton = new Button( "GP robot" );
	c.gridx = 0;
	c.gridy = 1;
	layout.setConstraints( tron.pickGPButton,c );
	tron.add( tron.pickGPButton );
	
	tron.pickNNButton = new Button( "NN robot" );
	c.gridx = 1;
	c.gridy = 1;
	layout.setConstraints( tron.pickNNButton,c );
	tron.add( tron.pickNNButton );
	
	tron.pickMyButton = new Button( "My robot" );
	c.gridx = 2;
	c.gridy = 1;
	layout.setConstraints( tron.pickMyButton,c );
	tron.add( tron.pickMyButton );
	
	tron.startButton = new Button( "start" );
	c.gridx = 3;
	c.gridy = 1;
	layout.setConstraints( tron.startButton,c );
	tron.add( tron.startButton );
	tron.startButton.disable();
	
	tron.quitButton = new Button( "quit" );
	c.gridx = 4;
	c.gridy = 1;
	layout.setConstraints( tron.quitButton,c );
	tron.add( tron.quitButton );
	
	tron.statusLabel = new 
	    Label( "select robot opponent, then press 'start'         " );
	c.gridx = 0;
	c.gridy = 2;
	c.gridwidth = 5;
	layout.setConstraints( tron.statusLabel,c );
	tron.add( tron.statusLabel );
	
	tron.pack();
	tron.show();
	tron.arena.start();

    } /* end of main() */

    
    
    /**
     * updateScore()
     *
     */
    public void updateScore() {
	robotScore = arena.player1.score;
	humanScore = arena.player2.score;
	statusLabel.setText( "robot: [" + robotScore + "]  "+
			     "human: [" + humanScore + "]" );
    } /* end of updateScore() */


    
    /**
     * start()
     *
     */
    public void start() {
	arena.start();
    } /* end of start() */

    
    
    /**
     * stop()
     *
     */
    public void stop() {
	arena.stop();
    } /* end of stop() */ 

    
    
    /**
     * destroy()
     *
     */
    public void destroy() {
    } /* end of destroy() */

    
    
    /**
     * handleEvent()
     *
     */
    public boolean handleEvent( Event evt ) {
	if ( evt.id == Event.WINDOW_DESTROY ) {
	    System.exit( 1 );
	    return true;
	}
	return super.handleEvent( evt );
    } /* end of handleEvent() */

    
    
    /**
     * action()
     *
     */
    public boolean action( Event evt,Object arg ) {
	if ( arg.equals( "quit" )) {
	    System.exit( 1 );
	    return true;
	}
	else if ( arg.equals( "start" )) {
	    arena.startAgain = true;
	    return true;
	}
	else if ( arg.equals( "GP robot" )) {
	    statusLabel.setText( "robot will be controlled by GP" );
	    player1 = GP;
	    arena.selectPlayer1( player1,gpfile );
	    startButton.enable();
	    return true;
	}
	else if ( arg.equals( "NN robot" )) {
	    statusLabel.setText( "robot will be controlled by NN" );
	    player1 = NN;
	    arena.selectPlayer1( player1,nnfile );
	    startButton.enable();
	    return true;
	}
	else if ( arg.equals( "My robot" )) {
	    statusLabel.setText( "robot will be controlled by my code" );
	    player1 = MY;
	    arena.selectPlayer1( player1,nnfile );
	    startButton.enable();
	    return true;
	}
	return false;
    } /* end of action() */
    

    
    /**
     * keyDown()
     *
     */
    public boolean keyDown( Event e,int key ) {
	if ( player2 == HUMAN ) {
	    switch ( key ) {
	    case Event.UP:
	    case (int) '8':
	    case (int) 'w':
		arena.player2.d = NORTH;
		arena.player2.didsomething = true;
		break;
	    case Event.DOWN:
	    case (int) '2':
	    case (int) 'z':
		arena.player2.d = SOUTH;
		arena.player2.didsomething = true;
		break;
	    case Event.LEFT:
	    case (int) '4':
	    case (int) 'a':
		arena.player2.d = WEST;
		arena.player2.didsomething = true;
		break;
	    case Event.RIGHT:
	    case (int) '6':
	    case (int) 's':
		arena.player2.d = EAST;
		arena.player2.didsomething = true;
		break;
	    }
	    return true;
	}
	else {
	    return false;
	}
    } /* end of keyDown() */

    
    
} /* end of Tron class */
Ultima modifica effettuata da sheva7 17/01/13 10:55
aaa
17/01/13 20:04
lorenzo
scusa la mia ignoranza...ma cosa ci dovrebbe essere da fixare?
Non penserai mica che uno si possa mettere ad analizzare tutto quel codice per capire cosa non va vero??

Almeno dicci cosa non funziona!
aaa
18/01/13 16:42
sheva7
Ok..
Intanto, Eclipse mi segnala errori per ogni istanza di gpplayer e nnplayer su Arena.java
aaa
20/01/13 21:05
Tw1st3r_Ev0
Devi importare l'intero progetto con tutti i sorgenti del gioco altrimenti ti da errori a cavolo. Se lo hai già fatto allora prova a risolvere gli errori delle istanze perchè penso saranno quelli che ti fanno generare errori a cascata. ;)
aaa
21/01/13 0:24
sheva7
dopo aver importato tutto, mi da questi errori:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    GPPlayer cannot be resolved to a type
    NNPlayer cannot be resolved to a type
    GPPlayer cannot be resolved to a type
    GPPlayer cannot be resolved to a type
    NNPlayer cannot be resolved to a type
    NNPlayer cannot be resolved to a type
    GPPlayer cannot be resolved to a type
    GPPlayer cannot be resolved to a type
    NNPlayer cannot be resolved to a type
    NNPlayer cannot be resolved to a type

    at Arena.<init>(Arena.java:30)
    at Tron.main(Tron.java:55)
aaa
21/01/13 11:44
Tw1st3r_Ev0
Non saprei dirti nel dettaglio il motivo di questi errori, forse sono dovuti al fatto che non hai importato per bene i sorgenti su un progetto perchè se dici che il codice per intero risulta esatto, allora non dovrebbe darti ulteriori errori (massimo qualche warning banale)! Prova a creare un nuovo progetto e poi cominci a trasferire per bene secondo come dovrebbero essere strutturati, tutti i sorgenti :)
Per esperienze personali, questi errori su eclipse li risolvevo cosi quando scaricavo sorgenti sul web, in caso se hai un altro IDE come NetBeans prova a fare la stessa procedura. :k:
aaa
22/01/13 18:19
sheva7
i sorgenti li ho importati tutti ma a quanto pare il codice ha dei problemi..
Eclipse mi ha detto che sono questi:

GPPlayer cannot be resolved to a type
NNPlayer cannot be resolved to a type

ma non riesco a capire cosa c'è che non va :d
aaa
23/01/13 17:54
Tw1st3r_Ev0
Un mio consiglio è, a sto punto, di cercare un altro sorgente dello stesso gioco ma magari già strutturato sotto forma di file progetto. O sennò vedi se ne trovi qualcun'altro senza errori.
Comunque non è una cosa mai accaduta su sorgenti presi dal web, la maggior parte li fanno apposta per non essere modificati facilmente, infatti basta anche solo sfasare una variabile per far "incazzare" il compilatore che ti segnala una decina o ventina di errori. xD
Prova ad aspettare che risponda qualcuno piu esperto di me in Java, essendo da poco approdato in questo linguaggio particolarmente complicato, alcune cose le devo ancora imparare. :yup:
aaa