sadeq
Orientation
Posts: 13
|
Share
Dec 25, 2005 23:18:34 GMT 3
Post by sadeq on Dec 25, 2005 23:18:34 GMT 3
For those who can access the students' local network, check my share:
\\Evid\Games Development\
right now, this folder contains some Design Documents samples and template. It also contains Wrath Lands document and the game itself (I'll try to get its video tutorials asap).
If someone wants to share anything, please post your share-name here.
|
|
|
Share
Dec 26, 2005 1:38:05 GMT 3
Post by admin on Dec 26, 2005 1:38:05 GMT 3
I here by make this post sticky! Thank you sadeq for this topic, I was going to start one but you beat me to it. This topic will be used to share open-source games code! dont share the game, share the code! here is my share, my first graphic game! OXO (Tic-Tac-Toe). Since it was my first game, there is a bug's' in it, but its working fine, just compile and play!
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import java.util.Random;
public class TTT extends JFrame implements ActionListener{ int[][] location =new int[3][3]; //inaitual values of t,tiecounter. int t=0; int tieCountar=0; int myTurn=0; int move=0; //adding the buttons to the board! private JButton jb0= new JButton(""); private JButton jb1= new JButton(""); private JButton jb2= new JButton(""); private JButton jb3= new JButton(""); private JButton jb4= new JButton(""); private JButton jb5= new JButton(""); private JButton jb6= new JButton(""); private JButton jb7= new JButton(""); private JButton jb8= new JButton(""); //adding labels to showthe X & O private JLabel jl0=new JLabel(" "); private JLabel jl1=new JLabel(" "); private JLabel jl2=new JLabel(" "); private JLabel jl3=new JLabel(" "); private JLabel jl4=new JLabel(" "); private JLabel jl5=new JLabel(" "); private JLabel jl6=new JLabel(" "); private JLabel jl7=new JLabel(" "); private JLabel jl8=new JLabel(" "); private Font font= new Font("SansSerif",Font.BOLD,55); public TTT(){ Container board=getContentPane(); setTitle("Tic Tac Toe"); //GridLayout to resimple the Tic-Tac-Toe board board.setLayout(new GridLayout(0,3,3,3)); board.add(jb0); board.add(jb1); board.add(jb2); board.add(jb3); board.add(jb4); board.add(jb5); board.add(jb6); board.add(jb7); board.add(jb8); jb0.setBackground(Color.white); jb1.setBackground(Color.white); jb2.setBackground(Color.white); jb3.setBackground(Color.white); jb4.setBackground(Color.white); jb5.setBackground(Color.white); jb6.setBackground(Color.white); jb7.setBackground(Color.white); jb8.setBackground(Color.white);
//adding labels to the buttons jb0.add(jl0); jl0.setFont(font); jb1.add(jl1); jl1.setFont(font); jb2.add(jl2); jl2.setFont(font); jb3.add(jl3); jl3.setFont(font); jb4.add(jl4); jl4.setFont(font); jb5.add(jl5); jl5.setFont(font); jb6.add(jl6); jl6.setFont(font); jb7.add(jl7); jl7.setFont(font); jb8.add(jl8); jl8.setFont(font); //adding listeners to the buttons jb0.addActionListener(this); jb1.addActionListener(this); jb2.addActionListener(this); jb3.addActionListener(this); jb4.addActionListener(this); jb5.addActionListener(this); jb6.addActionListener(this); jb7.addActionListener(this); jb8.addActionListener(this); setSize(300,300); setLocation(300,200); setResizable(false); setVisible(true);
} //a seconde constractor to take data from the previse game
public TTT(int a,Point xy,int o){ location =new int[3][3]; tieCountar=0; t=a; move=0; Container board=getContentPane(); //GridLayout to resimple the Tic-Tac-Toe board board.setLayout(new GridLayout(0,3,3,3)); board.add(jb0); board.add(jb1); board.add(jb2); board.add(jb3); board.add(jb4); board.add(jb5); board.add(jb6); board.add(jb7); board.add(jb8); jb0.setBackground(Color.white); jb1.setBackground(Color.white); jb2.setBackground(Color.white); jb3.setBackground(Color.white); jb4.setBackground(Color.white); jb5.setBackground(Color.white); jb6.setBackground(Color.white); jb7.setBackground(Color.white); jb8.setBackground(Color.white); jb0.add(jl0); jl0.setFont(font); jb1.add(jl1); jl1.setFont(font); jb2.add(jl2); jl2.setFont(font); jb3.add(jl3); jl3.setFont(font); jb4.add(jl4); jl4.setFont(font); jb5.add(jl5); jl5.setFont(font); jb6.add(jl6); jl6.setFont(font); jb7.add(jl7); jl7.setFont(font); jb8.add(jl8); jl8.setFont(font); //adding listeners to the buttons jb0.addActionListener(this); jb1.addActionListener(this); jb2.addActionListener(this); jb3.addActionListener(this); jb4.addActionListener(this); jb5.addActionListener(this); jb6.addActionListener(this); jb7.addActionListener(this); jb8.addActionListener(this); setSize(300,300); //to start at the exacte location of the previes one setLocation(xy); setResizable(false); setVisible(true); if(o==1){myTurn();}
}
public static void main(String[] args){ new TTT(); } public void actionPerformed(ActionEvent e){ if (e.getSource() == jb0) { int turn=turn(); setLocation(0,0,turn); jl0.setText(icon(turn)); checkLocation(turn); jb0.removeActionListener(this); jb0.setEnabled(false); if(turn==1){myTurn();} } if (e.getSource() == jb1) { int turn=turn(); setLocation(0,1,turn); jl1.setText(icon(turn)); checkLocation(turn); jb1.removeActionListener(this); jb1.setEnabled(false); if(turn==1){myTurn();} } if (e.getSource() == jb2) { int turn=turn(); setLocation(0,2,turn); jl2.setText(icon(turn)); checkLocation(turn); jb2.removeActionListener(this); jb2.setEnabled(false); if(turn==1){myTurn();} } if (e.getSource() == jb3) { int turn=turn(); setLocation(1,0,turn); jl3.setText(icon(turn)); checkLocation(turn); jb3.removeActionListener(this); jb3.setEnabled(false); if(turn==1){myTurn();} } if (e.getSource() == jb4) { int turn=turn(); setLocation(1,1,turn); jl4.setText(icon(turn)); checkLocation(turn); jb4.removeActionListener(this); jb4.setEnabled(false); if(turn==1){myTurn();} } if (e.getSource() == jb5) { int turn=turn(); setLocation(1,2,turn); jl5.setText(icon(turn)); checkLocation(turn); jb5.removeActionListener(this); jb5.setEnabled(false); if(turn==1){myTurn();} // } if (e.getSource() == jb6) { int turn=turn(); setLocation(2,0,turn); jl6.setText(icon(turn)); checkLocation(turn); jb6.removeActionListener(this); jb6.setEnabled(false); if(turn==1){myTurn();} } if (e.getSource() == jb7) { int turn=turn(); setLocation(2,1,turn); jl7.setText(icon(turn)); checkLocation(turn); jb7.removeActionListener(this); jb7.setEnabled(false); if(turn==1){myTurn();} } if (e.getSource() == jb8) { int turn=turn(); setLocation(2,2,turn); jl8.setText(icon(turn)); checkLocation(turn); jb8.removeActionListener(this); jb8.setEnabled(false); if (turn==1){myTurn();} } } // swithching turns between X & O public int turn(){ int a=t%2; t++; tieCountar++; return a+1; } // put the move in the array public void setLocation(int x,int y, int turn){ location[x][y]=turn; } //check the array if there is any wins or tie public void checkLocation(int turn){ if(((location[0][0]==turn)&&(location[0][1]==turn)&&(location[0][2]==turn)) ||((location[1][0]==turn)&&(location[1][1]==turn)&&(location[1][2]==turn)) ||((location[2][0]==turn)&&(location[2][1]==turn)&&(location[2][2]==turn)) ||((location[0][0]==turn)&&(location[1][0]==turn)&&(location[2][0]==turn)) ||((location[0][1]==turn)&&(location[1][1]==turn)&&(location[2][1]==turn)) ||((location[0][2]==turn)&&(location[1][2]==turn)&&(location[2][2]==turn)) ||((location[0][0]==turn)&&(location[1][1]==turn)&&(location[2][2]==turn)) ||((location[0][2]==turn)&&(location[1][1]==turn)&&(location[2][0]==turn))){ if (turn==1){ JOptionPane.showMessageDialog(new JFrame(),"X WIN","Tic Tac Toe",JOptionPane.PLAIN_MESSAGE ); new TTT(t,getLocationOnScreen(),1); dispose(); } else if (turn==2){ JOptionPane.showMessageDialog(new JFrame() ,"O WIN","Tic Tac Toe",JOptionPane.PLAIN_MESSAGE); new TTT(t,getLocationOnScreen(),0); dispose(); } } else if(tieCountar==9){ JOptionPane.showMessageDialog(new JFrame() ,"Tie","Tic Tac Toe",JOptionPane.PLAIN_MESSAGE); new TTT(t,getLocationOnScreen(),turn); dispose(); } } //Send X & O to the label of the button public String icon(int a){
if (a==1){ return " X"; } else{ return " O";} } // the computer turn and what to do public void myTurn(){ //try to win if(location[0][0]==0&&location[0][1]==2&&location[0][2]==2){ jb0.doClick() ; } else if(location[0][0]==0&&(location[1][0]==2)&&(location[2][0]==2)){ jb0.doClick() ; } else if(location[0][0]==0&&(location[1][1]==2)&&(location[2][2]==2)){ jb0.doClick() ; } // else if(location[0][1]==0&&(location[0][0]==2)&&(location[0][2]==2)){ jb1.doClick() ; } else if(location[0][1]==0&&(location[1][1]==2)&&(location[2][1]==2)){ jb1.doClick() ; } // else if(location[0][2]==0&&(location[0][1]==2)&&(location[0][0]==2)){ jb2.doClick() ; } else if(location[0][2]==0&&(location[1][2]==2)&&(location[2][2]==2)){ jb2.doClick() ; } else if(location[0][2]==0&&(location[1][1]==2)&&(location[2][0]==2)){ jb2.doClick() ; } // else if(location[1][0]==0&&(location[0][0]==2)&&(location[2][0]==2)){ jb3.doClick() ; } else if(location[1][0]==0&&(location[1][1]==2)&&(location[1][2]==2)){ jb3.doClick() ; } // else if(location[1][1]==0&&(location[1][0]==2)&&(location[1][2]==2)){ jb4.doClick() ; } else if(location[1][1]==0&&(location[0][1]==2)&&(location[2][1]==2)){ jb4.doClick() ; } else if(location[1][1]==0&&(location[0][2]==2)&&(location[2][0]==2)){ jb4.doClick() ; } else if(location[1][1]==0&&(location[0][0]==2)&&(location[2][2]==2)){ jb4.doClick() ; } // else if(location[1][2]==0&&(location[0][2]==2)&&(location[2][2]==2)){ jb5.doClick() ; } else if(location[1][2]==0&&(location[1][0]==2)&&(location[1][1]==2)){ jb5.doClick() ; } // else if(location[2][0]==0&&(location[0][0]==2)&&(location[1][0]==2)){ jb6.doClick() ; } else if(location[2][0]==0&&(location[2][1]==2)&&(location[2][2]==2)){ jb6.doClick() ; } else if(location[2][0]==0&&(location[1][1]==2)&&(location[0][2]==2)){ jb6.doClick() ; } // else if(location[2][1]==0&&(location[0][1]==2)&&(location[1][1]==2)){ jb7.doClick() ; } else if(location[2][1]==0&&(location[2][0]==2)&&(location[2][2]==2)){ jb7.doClick() ; } // else if(location[2][2]==0&&(location[0][2]==2)&&(location[1][2]==2)){ jb8.doClick() ; } else if(location[2][2]==0&&(location[2][0]==2)&&(location[2][1]==2)){ jb8.doClick() ; } else if(location[2][2]==0&&(location[1][1]==2)&&(location[0][0]==2)){ jb8.doClick() ; } // //if win is not possible, try to block else if(location[0][0]==0&&(location[0][1]==1)&&(location[0][2]==1)){ jb0.doClick() ; } else if(location[0][0]==0&&(location[1][0]==1)&&(location[2][0]==1)){ jb0.doClick() ; } else if(location[0][0]==0&&(location[1][1]==1)&&(location[2][2]==1)){ jb0.doClick() ; } // else if(location[0][1]==0&&(location[0][0]==1)&&(location[0][2]==1)){ jb1.doClick() ; } else if(location[0][1]==0&&(location[1][1]==1)&&(location[2][1]==1)){ jb1.doClick() ; } // else if(location[0][2]==0&&(location[0][1]==1)&&(location[0][0]==1)){ jb2.doClick() ; } else if(location[0][2]==0&&(location[1][2]==1)&&(location[2][2]==1)){ jb2.doClick() ; } else if(location[0][2]==0&&(location[1][1]==1)&&(location[2][0]==1)){ jb2.doClick() ; } // else if(location[1][0]==0&&(location[0][0]==1)&&(location[2][0]==1)){ jb3.doClick() ; } else if(location[1][0]==0&&(location[1][1]==1)&&(location[1][2]==1)){ jb3.doClick() ; } // else if(location[1][1]==0&&(location[1][0]==1)&&(location[1][2]==1)){ jb4.doClick() ; } else if(location[1][1]==0&&(location[0][1]==1)&&(location[2][1]==1)){ jb4.doClick() ; } else if(location[1][1]==0&&(location[0][2]==1)&&(location[2][0]==1)){ jb4.doClick() ; } else if(location[1][1]==0&&(location[0][0]==1)&&(location[2][2]==1)){ jb4.doClick() ; } // else if(location[1][2]==0&&(location[0][2]==1)&&(location[2][2]==1)){ jb5.doClick() ; } else if(location[1][2]==0&&(location[1][0]==1)&&(location[1][1]==1)){ jb5.doClick() ; } // else if(location[2][0]==0&&(location[0][0]==1)&&(location[1][0]==1)){ jb6.doClick() ; } else if(location[2][0]==0&&(location[2][1]==1)&&(location[2][2]==1)){ jb6.doClick() ; } else if(location[2][0]==0&&(location[1][1]==1)&&(location[0][2]==1)){ jb6.doClick() ; } // else if(location[2][1]==0&&(location[0][1]==1)&&(location[1][1]==1)){ jb7.doClick() ; } else if(location[2][1]==0&&(location[2][0]==1)&&(location[2][2]==1)){ jb7.doClick() ; } // else if(location[2][2]==0&&(location[0][2]==1)&&(location[1][2]==1)){ jb8.doClick() ; } else if(location[2][2]==0&&(location[2][0]==1)&&(location[2][1]==1)){ jb8.doClick() ; } else if(location[2][2]==0&&(location[1][1]==1)&&(location[0][0]==1)){ jb8.doClick() ; } // //can't win or block, random choise else{ JButton[] B={jb4,jb2,jb5,jb3,jb1,jb0,jb6,jb7,jb8}; Random g=new Random(); while(move<10){ int r=g.nextInt(9); if(B[r].isEnabled()){ B[r].doClick() ; move=0; break; } move++; } } } }
I have to say........... this is one bad written code! there is alot of refactoring that need to be done.
as you can see, the game loop is done at the last few lines of the code, its no controlled by a thread in this case, just by user clicks!
|
|
|
Share
Dec 27, 2005 14:48:20 GMT 3
Post by ali on Dec 27, 2005 14:48:20 GMT 3
I tried it...good one Ma'an ...thx
|
|
|
Share
Feb 15, 2006 19:12:05 GMT 3
Post by albara on Feb 15, 2006 19:12:05 GMT 3
Thank u sadeq ,,
i have downloaded the videos ,, thay r cooooool but the problem is how can i upload them .. ^_^
|
|
sadeq
Orientation
Posts: 13
|
Share
Mar 10, 2006 10:24:54 GMT 3
Post by sadeq on Mar 10, 2006 10:24:54 GMT 3
you can share them on the network
|
|
|
Share
Mar 13, 2006 13:09:15 GMT 3
Post by albara on Mar 13, 2006 13:09:15 GMT 3
but the problem is that i cannot access the network but i can put them in a CD and give to any one who is in the network .. any one how want them just let me know ..
|
|
|
Share
Mar 13, 2006 13:26:14 GMT 3
Post by albara on Mar 13, 2006 13:26:14 GMT 3
yah and thank u ma3an for the game .. its Working fine ,,
but tooooo HARD ^_^ ,, u cannot win on the computer ;D ..
|
|
|
Share
Mar 13, 2006 20:27:37 GMT 3
Post by admin on Mar 13, 2006 20:27:37 GMT 3
you can win, you just have to trick it in the first 1 or 2 moves, past that its over, he wins or a draw.
*side note* for the love of god, no one should every program like I did this one, its poorly structered, the style is horrible and it needs alot of refactoring! remeber, writing a clean code with comments help you and others understand what you want to do in the first place, so its easier to get help or to fix your own code.
|
|