PDA

View Full Version : My Java Projects



Trifonius
01-15-2008, 04:09 PM
http://www.trifonius.gghacks.com/java/driedee.html
http://www.trifonius.gghacks.com/java/snake/

these are my first 2 java applets :D
I Forgot to add dubble buffering in the first one, and didn't fix the coloring of the faces.
but it works, wohoo ;)

Yakman
01-15-2008, 04:32 PM
very nice :)
i started playing that snake game and found it hard to stop for a while.

any chance of the source?

Trifonius
01-15-2008, 06:22 PM
sure, i'll upload it tonight.
Btw, the source is even better, it has some more options that i couldn't get to work on the internet... weird

Dan Cardin
01-15-2008, 07:16 PM
like the cube :)

i think the going through walls is cool in snakes :) got me baffled the first time i hit one :p

EDIT: i just put the speed up to 100 and got 400 before it was even possible for me to hit my tail:P

Rikje
01-15-2008, 08:23 PM
I like the both!! :)
maby at a restart button to the snake game (a)

Trifonius
01-15-2008, 09:02 PM
the cube source(better version than the one i posted):

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class driedee extends Applet implements MouseMotionListener {

int xpos;
int ypos;
int p=3, k=10;
int pos=100;
int verg=40;

punt p1 = new punt(1,1,1);
punt p2 = new punt(1,1,-1);
punt p3 = new punt(1,-1,-1);
punt p4 = new punt(1,-1,1);
punt p5 = new punt(-1,1,1);
punt p6 = new punt(-1,1,-1);
punt p7 = new punt(-1,-1,-1);
punt p8 = new punt(-1,-1,1);
punt [] punten = {p1, p2, p3, p4, p5, p6, p7, p8};

vlak v1 = new vlak( p1, p2, p3, p4);
vlak v2 = new vlak( p5, p6, p7, p8);
vlak v3 = new vlak( p1, p2, p6, p5);
vlak v4 = new vlak( p4, p3, p7, p8);
vlak v5 = new vlak( p1, p5, p8, p4);
vlak v6 = new vlak( p2, p6, p7, p3);
vlak [] vlakken = { v1, v2, v3, v4, v5, v6};

int getx=0,gety=0;

public void init(){ addMouseMotionListener(this);
repaint();}

public void repaint() {
try{
Image img = this.createImage(getSize().width , getSize().height);
Graphics g = img.getGraphics();
g.setColor(this.getBackground());
g.fillRect(0, 0, getSize().width, getSize().height);
g.setColor(this.getForeground());
paint(g);
this.getGraphics().drawImage(img, 0, 0, null);
}
catch(NullPointerException e){}
}

public void paint(Graphics g){
v1.sorteerVlakken(vlakken);
for( int i=0; i!=6; i++){
Color c = new Color(255*i/6, 255*i/6, 240*i/6);
g.setColor(c);
g.fillPolygon(vlakken[i].rijX(), vlakken[i].rijY(),vlakken[i].rijX().length);
}

}



public void mouseMoved(MouseEvent me){


}

public void mouseDragged(MouseEvent me){
int x;
int y;

if((me.getX()-getx)<0) x=1;
else if ((me.getX()-getx)>0) x=-1;
else x=0;

if((me.getY()-gety)<0) y=1;
else if ((me.getY()-gety)>0) y=-1;
else y=0;

for(int i=0; i!=8; i++){
punten[i].draaiX(x);
punten[i].draaiY(y);
}

getx=me.getX();
gety=me.getY();

repaint();
}
}



class punt{
private int p=0,k=5,verg=40,pos=100;
public double xco;
public double yco;
public double zco;

public punt(double x, double y, double z){
xco=x;
yco=y;
zco=z;
}

public int p(){return p;}
public int k(){return k;}
public int verg(){return verg;}
public int pos(){return pos;}
public double x(){return xco;}
public double y(){return yco;}
public double z(){return zco;}



public int y2d(){
return (int)(((p-k)*xco)/(zco-k)*verg)+pos;
}
public int x2d(){
return (int)((((p-k)*yco)/(zco-k)*verg))+pos;
}

public void draaiX(double x){
x=x/30;
double xcot, ycot;
xcot=Math.cos(x)*xco-Math.sin(x)*yco;
ycot=Math.sin(x)*xco+Math.cos(x)*yco;
xco=xcot;
yco=ycot;
}

public void draaiY(double y){
y=y/30;
double xcot, zcot;
xcot=Math.cos(y)*xco-Math.sin(y)*zco;
zcot=Math.sin(y)*xco+Math.cos(y)*zco;
xco=xcot;
zco=zcot;
}
}

class vlak{
private punt a,b,c,d;
public vlak(punt a, punt b, punt c, punt d){
this.a=a;
this.b=b;
this.c=c;
this.d=d;
}
public double dieptev(){
return a.z()+b.z()+c.z()+d.z();
}
public int[] rijX(){
int[] rij = {a.x2d(),b.x2d(),c.x2d(),d.x2d()};
return rij;
}
public int[] rijY(){
int[] rij = {a.y2d(),b.y2d(),c.y2d(),d.y2d()};
return rij;
}
public void sorteerVlakken(vlak[] vlakken){
double [] dieptes = new double[vlakken.length];
for( int i=0; i!= vlakken.length; i++){
dieptes[i] = vlakken[i].dieptev();
}
for(int i=0; i!=vlakken.length-1; i++){
for(int j=0; j!=vlakken.length-1-i; j++){
if(dieptes[j]>dieptes[j+1]){
double tijdelijk = dieptes[j];
vlak tijdelijk2 = vlakken[j];
dieptes[j] = dieptes[j+1];
vlakken[j] = vlakken[j+1];
dieptes[j+1] = tijdelijk;
vlakken[j+1] = tijdelijk2;
}
}
}


}
}

Snake source:
All the names are in dutch, but added some comments to explain them


import java.awt.*;
import java.applet.*;
import java.awt.event.*;


public class snake extends Applet implements KeyListener, Runnable, ActionListener{

int lengte = 5; //Length of the snake at start
int fps = 15; //Speed
int blok =4; //Size of 1 block of the snake, change this to make game bigger/smaller, must be >3
int dim = 16; //Dimentions of the Field, the unit is 'blocks'
int verlenging =5; //Makes the snake X blocks longer when you eat some food

int frame;
int delay;
Thread animator;
int key;
int stap=blok+1; //Stap = Step = How many pixels the snake moves per frame
int x=stap,y=0;
slang s = new slang( 2*stap, 14*stap , lengte, stap, dim);
int wacht=0; //Wacht = Wait
Boolean GameOver=false;
int GameOverDraw=0;
int score=0;
int langer=0; //Langer = Longer
boolean gestart=false; //gestart = Started
Button sneller; //Sneller = Faster
Button trager; //Trager = Slower
Button start;
int auto, kies, kiesg; //kies = Choose

public void init(){
addKeyListener( this );
delay = (fps > 0) ? (1000 / fps) : 0;
sneller = new Button("Faster");
trager = new Button("Slower");
start = new Button("Start");
trager.addActionListener(this);
start.addActionListener(this);
sneller.addActionListener(this);
add(sneller);
add(trager);
add(start);

}

public void actionPerformed(ActionEvent evt){
if (evt.getSource() == sneller){fps++; delay = (fps > 0) ? (1000 / fps) : 0;repaint();}
if (evt.getSource() == trager && fps!=1){fps--; delay = (fps > 0) ? (1000 / fps) : 0;repaint();}
if (evt.getSource() == start) {gestart=true;
remove(start);
remove(trager);
remove(sneller);
init();
}
}

public void start() {
animator = new Thread(this);
animator.start();
}

public void run() {
while (Thread.currentThread() == animator) {

if (gestart==false && auto==4){
kies=(int)(Math.random()*4);
if (kies==0 && x!=stap) { x=-stap; y=0;}
if (kies==1 && x!=-stap) { x=stap; y=0;}
if (kies==2 && y!=stap) { y=-stap; x=0;}
if (kies==3 && y!=-stap) { y=stap; x=0;}
kiesg=kies;
auto=2;//(int)(10-(Math.random()*5));
}
auto++;
s.wijzigxCo(x, y);
s.wijzigyCo(y, langer, verlenging);
langer = 0;

if(s.checkPos()==1) {GameOver=true; x=0; y=0; GameOverDraw++;}
if(s.xe==(s.xCo)/stap && s.ye ==(s.yCo)/stap && gestart == true) {score+=fps; s.maakEten(); langer=verlenging;}

if(GameOver==false || GameOverDraw==1){if(wacht != 0) wacht--;repaint();}
try {
Thread.sleep(delay);
} catch (InterruptedException e) {break;}
frame++;

}
}

public void stop() {animator = null;}

public void keyTyped( KeyEvent e ) {}
public void keyReleased( KeyEvent e ) {}
public void keyPressed( KeyEvent e ) {
if(GameOver==false && wacht==0){
if(e.getKeyCode()==KeyEvent.VK_LEFT && x!=stap){
x=-stap; y=0;wacht=1;
}

if(e.getKeyCode()==KeyEvent.VK_RIGHT && x!=-stap){
x=stap; y=0;wacht=1;
}

if(e.getKeyCode()==KeyEvent.VK_UP && y!=stap){
y=-stap; x=0;wacht=1;
}

if(e.getKeyCode()==KeyEvent.VK_DOWN && y!=-stap){
y=stap; x=0;wacht=1;
}


}
}


public void repaint() {
try{
Image img = this.createImage(getSize().width , getSize().height);
Graphics g = img.getGraphics();
g.setColor(this.getBackground());
g.fillRect(0, 0, getSize().width, getSize().height);
g.setColor(this.getForeground());
paint(g);
this.getGraphics().drawImage(img, 0, 0, null);
}
catch(NullPointerException e){}
}
public void paint(Graphics g){

for(int i=0; i!=s.lengte; i+=1){
int il = (int)(((double)(i)/s.lengte)*255);
int a=255-il;
int b=0;
int c=il;
g.setColor(Color.gray);
g.fillRect( s.sgX(i)+2, s.sgY(i)+2, blok-1, blok-1);
g.setColor(new Color(a,b,c));
g.fillRect( s.sgX(i), s.sgY(i), blok, blok);
g.setColor(Color.black);
g.drawRect( s.sgX(i), s.sgY(i), blok-1, blok-1);
g.setColor(Color.white);
g.fillRect( s.sgX(i), s.sgY(i), 1, 1);
g.fillRect( s.sgX(i)+blok-1, s.sgY(i), 1, 1);
g.fillRect( s.sgX(i), s.sgY(i)+blok-1, 1, 1);
g.fillRect( s.sgX(i)+blok, s.sgY(i)+blok, 1, 1);
g.setColor(Color.gray);
g.fillRect( s.sgX(i)+blok-1, s.sgY(i)+blok-1, 1, 1);
}

g.setColor(Color.gray);
g.fillRect( s.xeten+2, s.yeten+2, blok-1, blok-1);
g.setColor(Color.GREEN);
g.fillRect( s.xeten, s.yeten, blok, blok);
g.setColor(Color.black);
g.drawRect( s.xeten, s.yeten, blok-1, blok-1);
g.setColor(Color.white);
g.fillRect( s.xeten, s.yeten, 1, 1);
g.fillRect( s.xeten+blok-1, s.yeten, 1, 1);
g.fillRect( s.xeten, s.yeten+blok-1, 1, 1);
g.fillRect( s.xeten+blok, s.yeten+blok, 1, 1);
g.setColor(Color.gray);
g.fillRect( s.xeten+blok-1, s.yeten+blok-1, 1, 1);

g.setColor(Color.black);
g.drawString("Length: " + ((s.sg).length()/8) + " | Score: " + score + " | Trifonius Snake!" ,2,dim*stap+20);
g.drawString("Speed: " + fps, 2, dim*stap+36);
g.drawString("Turn = Arrow Keys | King-Size mode for Knollivier", 2, dim*stap+52);
g.drawRect(0, 0, dim*stap, dim*stap);

if(GameOver==true){
g.setColor(Color.red);
g.fillRect(((int)(2*dim/3))*stap, ((int)(dim/2))*stap, 75, 17);
g.setColor(Color.blue);
g.drawString("Game Over!", ((int)(2*dim/3))*stap+5, ((int)(dim/2))*stap+13);
}
}

}


class slang{
public String sg="", sx,sy; // sg = String containing the x and y co of all the blocks of the snake
public int xCo, yCo;
public int dim;
public int xeten,yeten, xe, ye;
int slanger=0;
int lengte;
int stap;
int[][]checkPos;

public slang(int x, int y, int l, int st, int dime){ //Slang = Snake
xCo=x;
yCo=y;
lengte=l;
stap=st;
for(int i=0; i!=lengte; i++){
if (xCo<100) sx="00"+xCo;
else sx="0"+xCo;
if (yCo<100) sy="00"+yCo;
else sy="0"+yCo;
sg=sg+sx+sy;
xCo+=stap;
}
dim=dime;
xCo-=stap;
int[][]checkPo=new int[dim][dim];
checkPos=checkPo;
for(int i=0; i!=dim; i++){
for(int u=0; u!=dim; u++){
checkPos[i][u]=0;
}
}
maakEten();
}

public void maakEten(){ //MaakEten = MakeFood
xe=((int)(Math.random()*dim));
ye=((int)(Math.random()*dim));
if(checkPos[xe][ye]==1) maakEten();
checkPos[xe][ye]=2;
xeten=xe*stap+1;
yeten=ye*stap+1;
}

public int xEten(){return xeten;} // = X Food
public int yEten(){return yeten;} // = Y Food

public int checkPos(){
int t=checkPos[(xCo)/stap][(yCo)/stap];
checkPos[(xCo)/stap][(yCo)/stap]=1;
checkPos[(sgX(0))/stap][(sgY(0))/stap]=0;
return t;
}

public void wijzigSg(int langer, int verlenging){ // = Change Sg
if(xCo<10) sx="000" + xCo;
else if (xCo<100) sx="00"+xCo;
else sx="0"+xCo;
if(yCo<10) sy="000" + yCo;
else if (yCo<100) sy="00"+yCo;
else sy="0"+yCo;
sg=sg+sx+sy;
if(langer==verlenging) slanger=langer;
if(slanger==0){
sg=sg.substring(8);
} else {slanger--; lengte++;}
}

public int sgX(int i){
return Integer.parseInt(sg.substring(i*8,i*8+4))+1;
}
public int sgY(int i){
return Integer.parseInt(sg.substring((i)*8+4,(i)*8+8))+1;
}

public int xCo(){
return xCo;}
public int yCo(){
return yCo;}

public void wijzigxCo(int x, int y){ //Change x Co
xCo=xCo+x;
if(xCo<0 && x<0) xCo+=stap*dim;
if(xCo>stap*(dim-1) && x>0) xCo-=stap*dim;
if(yCo<stap && y<0) yCo+=stap*dim;
if(yCo>stap*(dim-2) && y>0) yCo-=stap*dim;
}
public void wijzigyCo(int y, int langer, int verlanging){ //Change Y Co
yCo=yCo+y;
wijzigSg(langer, verlanging);
}


}


If you use/edit my code, please post the result here :)

Wizzup?
01-15-2008, 09:16 PM
Dutch? ;)

Trifonius
01-15-2008, 09:30 PM
Well, it's Flemish actually :p
*wiki: Flemish (linguistics), a number of varieties of the Dutch language

Booo NL Wohooo Be! ^^

King of Knives
01-16-2008, 09:53 AM
I'm learning Java... But it's going like hell. But thanks.:D It's never too soon to read source code ;).

-Knives

Trifonius
01-30-2008, 12:46 AM
My latest project: 'SuSiSuSo' by Trifonius (SuperSimpleSudokuSolver)
It solves EAZY sudoku's only, because i'm too lazy atm to implement a 'guessing' system, that is required for the harder sudokus.... Maybe i'll do it later

here it is :


//SuSiSuSo by Trifonius (SuperSimpleSudokuSolver)

public class SudokuSolver {

public static void main(String[] args) {
int[][] Geg = { { 4, 6, 0, 0, 3, 9, 0, 1, 0}, //put the Sudoku that has to be solved here
{ 1, 0, 7, 0, 6, 0, 9, 3, 8}, //Only eazy sudoku's!
{ 0, 9, 3, 7, 0, 0, 0, 0, 0}, //0=an empty box

{ 0, 0, 2, 0, 0, 0, 3, 0, 0},
{ 9, 7, 0, 0, 5, 0, 4, 0, 6},
{ 5, 0, 0, 0, 0, 7, 1, 9, 2},

{ 0, 8, 0, 4, 7, 6, 0, 0, 0},
{ 7, 1, 0, 0, 0, 5, 0, 0, 9},
{ 0, 5, 0, 0, 9, 8, 0, 6, 3} };
Sudo Sud = new Sudo(Geg);
Boolean Again;
do{
Again = false;
//System.out.println("jo");
for(int i=11; i!=100; i++){
if (i%10==0) i++;
int a = (i-(i%10))/10;
int b = i%10;
int ky = (int)(a+2)/3-1;
int kx = (int)(b+2)/3-1;
//System.out.println(a + ", " + b);
//System.out.println(ky + ", " + kx);
//System.out.println("----");
if ((Sud.Sudoku[a][b]).length()!=1){
Again=true;
//CheckRij
for(int u=1; u!=10; u++){
if ((u!=b) && (Sud.Sudoku[a][u].length()==1) && (Sud.Sudoku[a][b].indexOf(Sud.Sudoku[a][u])>=0)){
Sud.Sudoku[a][b] = Sud.Sudoku[a][b].substring(0, Sud.Sudoku[a][b].indexOf(Sud.Sudoku[a][u])) + Sud.Sudoku[a][b].substring( 1 + Sud.Sudoku[a][b].indexOf(Sud.Sudoku[a][u]));
//System.out.println(a + ", " +b);
}
}
//CheckKolom
for(int u=1; u!=10; u++){
if ((u!=a) && (Sud.Sudoku[u][b].length()==1) && (Sud.Sudoku[a][b].indexOf(Sud.Sudoku[u][b])>=0)){
Sud.Sudoku[a][b] = Sud.Sudoku[a][b].substring(0, Sud.Sudoku[a][b].indexOf(Sud.Sudoku[u][b])) + Sud.Sudoku[a][b].substring( 1 + Sud.Sudoku[a][b].indexOf(Sud.Sudoku[u][b]));
//System.out.println(a + ", " +b);
}
}
//Check3x3
//System.out.println("_______");
for(int u=1; u!=10; u++){
int ua=1+3*ky+(u-(u+2)%3)/3;
int ub=1+3*kx+(u+2)%3;
//System.out.println("u= " + u + ", ua= " + ua + ", ub= " + ub);
if ((ua!=a) && (ub!=b) && (Sud.Sudoku[ua][ub].length()==1) && (Sud.Sudoku[a][b].indexOf(Sud.Sudoku[ua][ub])>=0)){
Sud.Sudoku[a][b] = Sud.Sudoku[a][b].substring(0, Sud.Sudoku[a][b].indexOf(Sud.Sudoku[ua][ub])) + Sud.Sudoku[a][b].substring( 1 + Sud.Sudoku[a][b].indexOf(Sud.Sudoku[ua][ub]));
System.out.println(a + ", " +b);
}
}
}
}

}while(Again==true);
PrintSudoku(Sud);
}

public static void PrintSudoku(Sudo Sud){
for(int i=1; i!=10; i++){
String rij = " |";
for(int u=1; u!=10; u++){
String box = Sud.Sudoku[i][u];
do{
box = " " + box;
} while(box.length()!=10);
box = box + " |";
rij = rij + box;
}
System.out.println(rij);
}
//System.out.println("_________________");
}


}

class Sudo{
public String[][] Sudoku = new String[10][10];

public Sudo( int[][] Sud){
for(int i=11; i!=100; i++){
if (i%10==0) i++;;
Sudoku[(i-(i%10))/10][i%10] = Sud[(i-(i%10))/10-1][i%10-1]+"";
if (Integer.parseInt(Sudoku[(i-(i%10))/10][i%10])==0) Sudoku[(i-(i%10))/10][i%10]="123456789";
//System.out.println(Sudoku[(i-(i%10))/10][i%10]);
}
}
}

coo too
02-03-2008, 10:47 AM
That snake game is addicting