diff --git a/HandCricket.java b/HandCricket.java new file mode 100644 index 0000000..624b134 --- /dev/null +++ b/HandCricket.java @@ -0,0 +1,277 @@ +import java.io.*; + +public class Playing_Hand_Cricket_Against_Computer + +{ + + public static void main(String args[])throws IOException + + { + + InputStreamReader isr=new InputStreamReader(System.in); + BufferedReader br=new BufferedReader(isr); + + String name; // to accept the player's name; + int aa; // converting the random real number generated by 'rr' into a random whole number run for the computer; + int ii; // used in for loops; + int bb; // to accept the player's number; + double rr; // to generate a random real number; + int cheat; // to end the game if wrong input is entered; to keep a check whilst the game is running as to whether the game has ended or not; + int score1; // to store the latest runs scored by the player; + int score2; // to store the latest runs scored by the computer; + int c; // used in for loops to create a specific design; + int choice; // used to decide whether the game needs to be ended or restarted; + int infinity=1; // used to keep the game running until the player decides to end the game; + + /* After referring to all the comments mentioned above, + * you must be able to understand the working of this program as it is very easy. + * Have fun! + */ + + while(infinity>0) + { + + name=" "; + aa=0; + ii=0; + bb=0; + rr=0; + cheat=0; + score1=0; + score2=0; + c=0; + choice=0; + infinity=1; + + System.out.println("======================="); + System.out.println("| WELCOME TO THE GAME |"); + System.out.println("======================="); + System.out.println(""); + System.out.print("Enter Your Name: "); + name=br.readLine(); + + for(c=0;c<=2;c++) + { + System.out.println("===================================="); + } + + System.out.println("Hello "+name+","); + System.out.println("."); + System.out.println("WELCOME TO THE CRICKET-MANIA"); + System.out.println("."); + System.out.println("Instructions:"); + System.out.println("."); + System.out.println("This Game Is Similar To The Hand-Cricket Game Played By Kids."); + System.out.println("."); + System.out.println("There Is No Limit On The Number Of Balls."); + System.out.println("."); + System.out.println("You Will First Bat. Enter Any Number From 1 To 10."); + System.out.println("."); + System.out.println("Enter The Numbers Carefully Without Making Any Mistake."); + System.out.println("."); + System.out.println("If The Number Entered By The Computer Is Same As The Number Entered By You,"); + System.out.println("You Will Be Declared As Out."); + System.out.println("."); + System.out.println("Your Final Score Will Be The Sum Of The Numbers You Entered Before Getting Out."); + System.out.println("."); + System.out.println("After Getting Out, You Will Have To Bowl."); + System.out.println("."); + System.out.println("Try To Enter A Number Similar To The Number Entered By The Computer."); + System.out.println("."); + System.out.println("If The Sum Of Numbers Entered By The Computer Becomes More Than Your Score, It Will Win."); + System.out.println("."); + System.out.println("But If You Enter A Number Same As The Number Entered By The Computer, You Can Win!"); + System.out.println("."); + + for(c=0;c<=2;c++) + { + System.out.println("===================================="); + } + + System.out.println("YOU ARE BATTING:"); + + for(c=0;c<=2;c++) { System.out.println("===================================="); } for(ii=1;ii>0;ii++) + { + System.out.println(""); + System.out.print("Enter Your Number: "); + bb=Integer.parseInt(br.readLine()); + rr=((Math.random())*10); + rr=rr+1; + aa=(int)(rr); + System.out.println(""); + System.out.println("Computer's Number: "+aa); + System.out.println(""); + System.out.println("**********"); + System.out.println(""); + + if(bb==aa) + { + System.out.println(" "); + System.out.println("HOWZATTT!!!!!!"); + System.out.println(" "); + System.out.println("################"); + System.out.println("################"); + System.out.println("YOUR FINAL SCORE = "+score1); + System.out.println("################"); + System.out.println("################"); + System.out.println(" "); + break; + } + + else if(bb>0&&bb<=10) { score1=score1+bb; } else if(bb>10||bb<=0) + { + for(c=0;c<=2;c++) + { + System.out.println("===================================="); + } + System.out.println("You have either tried to cheat or entered a wrong input. Game Over!"); + cheat++; + break; + } + + System.out.println("Your Current Score Is "+score1); + System.out.println(""); + System.out.println("**********"); + } + + for(c=0;c<=2;c++) { if(cheat>0) + { + break; + } + System.out.println("===================================="); + } + + for(c=0;c<1;c++) { if(cheat>0) + { + break; + } + System.out.println("YOU ARE BOWLING:"); + } + + for(c=0;c<=2;c++) { if(cheat>0) + { + break; + } + System.out.println("===================================="); + } + + for(ii=1;ii>0;ii++) + { + if(cheat>0) + { + break; + } + + System.out.println(""); + System.out.print("Enter Your Number: "); + bb=Integer.parseInt(br.readLine()); + rr=((Math.random())*10); + rr=rr+1; + aa=(int)(rr); + System.out.println(""); + System.out.println("Computer's Number: "+aa); + System.out.println(""); + System.out.println("**********"); + System.out.println(""); + + if(aa==bb) + { + System.out.println(" "); + System.out.println("COMPUTER IS OUTT!!!!"); + System.out.println(" "); + System.out.println("################"); + System.out.println("################"); + System.out.println("COMPUTER'S FINAL SCORE = "+score2); + System.out.println("################"); + System.out.println("################"); + System.out.println(" "); + break; + } + + else if(bb>0&&bb<=10) { score2=score2+aa; } else if(bb>10||bb<=0) + { + for(c=0;c<=2;c++) { System.out.println("===================================="); } System.out.println("You have either tried to cheat or entered a wrong input. Game Over!"); cheat++; break; } if(score2>score1) + { + System.out.println(" "); + System.out.println("THE COMPUTER HAS SCORED MORE THAN YOUU!!!"); + System.out.println(" "); + System.out.println("################"); + System.out.println("################"); + System.out.println("COMPUTER'S FINAL SCORE = "+score2); + System.out.println("################"); + System.out.println("################"); + System.out.println(" "); + break; + } + + System.out.println("Computer's Current Score Is "+score2); + System.out.println(""); + System.out.println("**********"); + } + + for(c=0;c<=2;c++) { if(cheat>0) + { + break; + } + + System.out.println("===================================="); + } + + for(ii=0;ii<1;ii++) { if(cheat>0) + { + break; + } + + System.out.println(" "); + System.out.println("################"); + System.out.println("################"); + System.out.println("YOUR FINAL SCORE = "+score1); + System.out.println("COMPUTER'S FINAL SCORE = "+score2); + System.out.println("################"); + System.out.println("################"); + System.out.println(" "); + + if((score1)>(score2)) + { + System.out.println("Congratulations "+name+"!! You Have Defeated The Almighty, Computer!!"); + } + + else if((score1)<(score2)) + { + System.out.println("Sorry "+name+", But The Computer Has Defeated You..!"); + } + + else + { + System.out.println("It's a Tie..! :("); + } + + } + + for(c=0;c<=2;c++) + { + System.out.println("===================================="); + } + + System.out.println(""); + System.out.println("Enter 1 To Play This Game Again."); + System.out.println(""); + System.out.println("Enter 0 Or Any Other Number To Quit."); + choice=Integer.parseInt(br.readLine()); + System.out.println(""); + + if(choice==1) + { + System.out.print('\f'); + } + + else + { + System.out.print('\f'); + System.out.println("Good Bye!"); + break; + } + + } + } +} diff --git a/RapidRollGame.java b/RapidRollGame.java new file mode 100644 index 0000000..a264ff4 --- /dev/null +++ b/RapidRollGame.java @@ -0,0 +1,603 @@ +import java.awt.*; //CHANGE THE scrollSpeed value + + +import java.awt.event.*; //to run the thread with more + +import java.util.*; +import java.io.*; + +class HighSc +{ + + int hsc; + BufferedReader br; + FileInputStream fis; + String sths; + public String getHighScore() throws IOException + { + fis = new FileInputStream("highscore.txt"); + br = new BufferedReader(new InputStreamReader(fis)); + + sths = br.readLine(); + + fis.close(); + return sths; + } + + } + + +class GameFrame extends Frame implements Runnable, KeyListener +,ActionListener +{ + + + MenuBar mb; + Menu m1; + MenuItem mi1,mi2,mi3,mi4,mi5,mi6; + + Button b1,b2,b3; + Button ball; + Button oneUp; + + final int TOPLINE = 50; + final int BOTTOMLINE = 350; + final int LEFTLINE = 20; + final int RIGHTLINE = 350 ; + + int scrollSpeed = 25; + + int x=50, y=300; + int x2=125,y2=200; + int x3 = 90,y3=100; + int bx = 80; + int by = 239; + int ox = 0; + int oy = 0; + int score = 0; + int oneUpCount = 1; + + int t1,t2,t3; + + int chances=3; + int a,b,c; + int diff = 65; + String msg = ""; + String chns = ""; + String st = ""; + + + char ch; + int kcode; + boolean flagLKey = true; + boolean flagRKey = true; + boolean flagTop1 = false; + boolean flagTop2 = false; + boolean flagTop3 = false; + boolean flagDrop = true; + boolean flagMove = true; + boolean flagBetween = true; + boolean flagOnx = true; + boolean flagOnx2 = false; + boolean flagOnx3 = false; + boolean flagJump = true; + boolean flagNew1 = false; + boolean flagOneUp = false; + + Thread t; + + GameFrame() + { + + + mb = new MenuBar(); + m1 = new Menu("File"); + mi1 = new MenuItem("New Game"); + mi2 = new MenuItem("HighScores"); + mi3 = new MenuItem("Exit"); + + setMenuBar(mb); + mb.add(m1); + + m1.add(mi1); + m1.add(mi2); + m1.add(mi3); + + + + setTitle("simple frame"); + setSize(400,350); + + setLayout(null); + b1 = new Button(""); + b2 = new Button(""); + b3 = new Button(""); + ball = new Button("o"); + oneUp = new Button("0"); + add(ball); + add(b1); + add(b2); + add(b3); + add(oneUp); + b1.setBounds(x,y,70,20); + b2.setBounds(x2,y2,70,20); + b3.setBounds(x3,y3,70,20); + + ball.setBounds(bx,by,10,10); + oneUp.setBounds(bx-5,by,5,5); + + setBackground(Color.blue); + setForeground(Color.white); + ball.addKeyListener(this); + + mi1.addActionListener(this); + mi2.addActionListener(this); + mi3.addActionListener(this); + + addWindowListener(new WindowAdapter() + { + public void windowClosing(WindowEvent we) + { + closeAll(); + } + }); + + t = new Thread(this,"game"); + t.start(); + + } //end of constructor + + public void actionPerformed(ActionEvent ae) + { + st = ae.getActionCommand(); + if(st.equals("New Game")) + { + + stop();this.setVisible(false); + Frame ng = new GameFrame(); + ng.setVisible(true); + + } + else if(st.equals("HighScores")) + { + + HighSc hs = new HighSc(); + try + { + msg = ""; + msg = hs.getHighScore(); repaint(); + } + catch(IOException ie) + { + System.out.println(ie); + } + } + else if(st.equals("Exit")) + { + closeAll(); + } + + } + + public void closeAll() + { + + stop(); + t = null; + System.exit(0); + + } + + public void keyPressed(KeyEvent ke) + { + kcode = ke.getKeyCode(); + + + + + switch(kcode) + { + + case KeyEvent.VK_LEFT: + if(flagDrop) + bx -= 4; + + + + + +if(ox<=bx+2&&ox>=bx&&(oy>=by&&oy<=by+6)) + { + chances++;ox = 0;oy = 0; + repaint(); flagOneUp = false; + } + + while(flagLKey) + { + if(((bx>=x&&bx<=x+70)&&(by=y-11)) + ||((bx>=x2&&bx<=x2+70)&&(by=y2-11)) + ||((bx>=x3&&bx<=x3+70)&&(by=y3-11))) + + { + flagBetween = true; + bx -= 2; flagDrop = false; + } + else + { + flagDrop = true; + flagBetween = false; + if(flagOnx) + flagOnx = false; + else if(flagOnx2) + flagOnx2 = false; + else if(flagOnx3) + flagOnx3 = false; + + break; + } + + repaint(); break; + } + + + break; + + case KeyEvent.VK_RIGHT: + if(flagDrop) + bx +=4; + + +if(ox<=bx+2&&ox>=bx&&(oy>=by&&oy<=by+6)) + { + chances++; ox = 0; oy = 0; + repaint(); flagOneUp = false; + } + +while(flagRKey) + { + + if(((bx>=x&&bx<=x+70)&&(by=y-11)) + ||((bx>=x2&&bx<=x2+70)&&(by=y2-11)) + ||((bx>=x3&&bx<=x3+70)&&(by=y3-11))) + + { + bx += 2; + flagBetween = true; + flagDrop = false; + repaint(); break; + } + else + { + flagBetween = false; + flagDrop = true; + + if(flagOnx) + flagOnx = false; + else if(flagOnx2) + flagOnx2 = false; + else if(flagOnx3) + flagOnx3 = false; + + break; + } + } + break; + } + + } + public void keyReleased(KeyEvent ke) + { + if(bxBOTTOMLINE) + { + flagRKey = false; + flagLKey = true; + } + else + { + flagLKey = true; + flagRKey = true; + } + + + } + + public void keyTyped(KeyEvent ke) + { + + } + + public void flagDropFun() + { + + + while(flagDrop) + { + by += 1; + + if(((bx>=x&&bx<=x+70)&&(by=y-11)) + ||((bx>=x2&&bx<=x2+70)&&(by=y2-11)) + ||((bx>=x3&&bx<=x3+70)&&(by=y3-11))) + { + + flagDrop = false; + flagBetween = true; + + +if(!flagOnx2&&!flagOnx3&&(bx>=x&&bx<=x+70)&&(by=y-11)) + { + flagOnx = true; flagJump = true;score +=5; + flagOnx2 = false; + flagOnx3 = false; + } + else +if(!flagOnx&&!flagOnx3&&(bx>=x2&&bx<=x2+70)&&(by=y2-11)) + { + + flagOnx2 = true; flagJump = true; score += 5; + flagOnx = false; + flagOnx3 = false; + } + else +if(!flagOnx&&!flagOnx2&&(bx>=x3&&bx<=x3+70)&&(by=y3-11)) + { + flagOnx3 = true; flagJump = true; score +=5; + flagOnx = false; + flagOnx2 = false; + } + else + { + flagOnx = false; + flagOnx2 = false; + flagOnx3 = false; + + } + + } + + repaint(); break; + } + + } + + + public void run() + { + try + { + + while(flagMove) + { + + while(flagDrop) + { + flagDropFun();break; + } + + + a = (int)(Math.random()*100); + b = (int)(Math.random()*100); + c = (int)(Math.random()*10); + + + if(yBOTTOMLINE) + { + chances--; + flagNew1 = true; + flagDrop = false; + t.sleep(100); + if(chances == 0) + { + msg = "Game Over"; repaint(); + stop(); + } + + } + repaint(); break; + } + + /* if(score%50==0) + { + scrollSpeed -= 3; score += 5; + } */ + + repaint(); + + Thread.sleep(scrollSpeed); + + } //end of while flagMove + + } + catch(InterruptedException ie) + { + System.out.println(ie); + } + + } + public void stop() + { + flagMove = false; + t = null; + } + + public void paint(Graphics g) + { + b1.setBounds(x,y,70,20); + b2.setBounds(x2,y2,70,20); + b3.setBounds(x3,y3,70,20); + ball.setBounds(bx,by,10,10); + oneUp.setBounds(ox,oy,5,5); + g.drawString("Score :"+score,280,65); + g.drawString("Chances :"+chances,280,75); + + g.drawString(msg,100,100); + + } + + } // end of BFrame + +public class RapidRollGame +{ + public static void main(String []args) + { + Frame f1 = new GameFrame(); + f1.setVisible(true); + + } + + } diff --git a/TicTacToe.java b/TicTacToe.java new file mode 100644 index 0000000..24886a0 --- /dev/null +++ b/TicTacToe.java @@ -0,0 +1,284 @@ +import java.applet.Applet; +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Container; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; +import java.io.PrintStream; +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +public class GameApplet extends Applet +implements MouseListener, ActionListener, WindowListener +{ + JFrame f; + int flag = 2; + int n; + int m; + int i = 0; + static int bug = 0; + char[] ch = new char[9]; + JButton first; + JButton second; + String s1 = ""; + + public GameApplet() + { + this.f = new JFrame("Tic Tac Toe"); + this.first = new JButton("CLEAR"); + this.second = new JButton("EXIT"); + this.f.add(this.first); + this.f.add(this.second); + this.f.addWindowListener(this); + this.f.getContentPane().setBackground(Color.BLUE); + this.f.setLayout(null); + this.f.setVisible(true); + this.f.setSize(800, 600); + this.first.setBounds(650, 50, 90, 60); + this.second.setBounds(650, 250, 90, 60); + + this.f.addMouseListener(this); + for (this.i = 0; this.i < 9; this.i += 1) + this.ch[this.i] = 'B'; + this.first.addActionListener(this); + this.second.addActionListener(this); + + String message = "Please click on the frame !!!!! \n \nto start the game \n"; + + JOptionPane pane = new JOptionPane(message); + JDialog dialog = pane.createDialog(new JFrame(), "Dilaog"); + dialog.show(); + Graphics g = this.f.getGraphics(); + g.drawLine(200, 0, 200, 600); + g.drawLine(400, 0, 400, 600); + g.drawLine(0, 200, 600, 200); + g.drawLine(0, 400, 600, 400); + g.drawLine(600, 0, 600, 600); + } + + public void keyPressed(KeyEvent k) + { + System.out.print(""); + } + + public void keyTyped(KeyEvent k) { + this.s1 += k.getKeyChar(); + } + + public void keyReleased(KeyEvent k) { + System.out.print(""); + } + + public void actionPerformed(ActionEvent ae) + { + if (ae.getSource() == this.first) + { + this.f.setVisible(false); + bug = 0; + new GameApplet(); + } + if (ae.getSource() == this.second) + { + System.exit(0); + } + } + + public void windowClosing(WindowEvent de) + { + System.exit(0); } + + public void windowOpened(WindowEvent de) { } + + public void windowClosed(WindowEvent de) { } + + public void windowActivated(WindowEvent de) { } + + public void windowDeactivated(WindowEvent de) { } + + public void windowIconified(WindowEvent de) { } + + public void windowDeiconified(WindowEvent de) { } + + public void mouseClicked(MouseEvent e) { Graphics2D g2; + Graphics g = this.f.getGraphics(); + g.drawLine(200, 0, 200, 600); + g.drawLine(400, 0, 400, 600); + g.drawLine(0, 200, 600, 200); + g.drawLine(0, 400, 600, 400); + g.drawLine(600, 0, 600, 600); + this.flag -= 1; + int x = e.getX(); + int y = e.getY(); + if (this.flag == 1) + { + if ((x < 200) && (y < 200)) { this.m = 0; this.n = 0; this.ch[0] = 'R'; } + if ((x > 200) && (x < 400) && (y < 200)) { this.m = 200; this.n = 0; this.ch[1] = 'R'; } + if ((x > 400) && (x < 600) && (y < 200)) { this.m = 400; this.n = 0; this.ch[2] = 'R'; } + if ((x < 200) && (y > 200) && (y < 400)) { this.m = 0; this.n = 200; this.ch[3] = 'R'; } + if ((x > 200) && (x < 400) && (y > 200) && (y < 400)) { this.m = 200; this.n = 200; this.ch[4] = 'R'; } + if ((x > 400) && (x < 600) && (y > 200) && (y < 400)) { this.m = 400; this.n = 200; this.ch[5] = 'R'; } + if ((x < 200) && (y > 400) && (y < 600)) { this.m = 0; this.n = 400; this.ch[6] = 'R'; } + if ((x > 200) && (x < 400) && (y > 400) && (y < 600)) { this.m = 200; this.n = 400; this.ch[7] = 'R'; } + if ((x > 400) && (x < 600) && (y > 400) && (y < 600)) { this.m = 400; this.n = 400; this.ch[8] = 'R'; } + g.setColor(new Color(77, 176, 230)); + g2 = (Graphics2D)g; + g2.setStroke(new BasicStroke(10.0F)); + g.drawOval(this.m + 10, this.n + 10, 159, 159); + } + + if (this.flag == 0) + { + if ((x < 200) && (y < 200)) { this.m = 0; this.n = 20; this.ch[0] = 'P'; } + if ((x > 200) && (x < 400) && (y < 200)) { this.m = 200; this.n = 20; this.ch[1] = 'P'; } + if ((x > 400) && (x < 600) && (y < 200)) { this.m = 400; this.n = 20; this.ch[2] = 'P'; } + if ((x < 200) && (y > 200) && (y < 400)) { this.m = 0; this.n = 200; this.ch[3] = 'P'; } + if ((x > 200) && (x < 400) && (y > 200) && (y < 400)) { this.m = 200; this.n = 200; this.ch[4] = 'P'; } + if ((x > 400) && (x < 600) && (y > 200) && (y < 400)) { this.m = 400; this.n = 200; this.ch[5] = 'P'; } + if ((x < 200) && (y > 400) && (y < 600)) { this.m = 0; this.n = 400; this.ch[6] = 'P'; } + if ((x > 200) && (x < 400) && (y > 400) && (y < 600)) { this.m = 200; this.n = 400; this.ch[7] = 'P'; } + if ((x > 400) && (x < 600) && (y > 400) && (y < 600)) { this.m = 400; this.n = 400; this.ch[8] = 'P'; } + g2 = (Graphics2D)g; + g2.setStroke(new BasicStroke(10.0F)); + g.setColor(new Color(77, 176, 230)); + g.drawLine(this.m + 10, this.n + 13, this.m + 169, this.n + 164); + g.drawLine(this.m + 169, this.n + 10, this.m + 10, this.n + 169); + this.flag += 2; + } + + for (this.i = 0; this.i < 3; this.i += 1) + { + if ((this.ch[this.i] != 'B') && + (this.ch[(this.i + 3)] == this.ch[this.i]) && (this.ch[(this.i + 6)] == this.ch[this.i])) + { + new Board().win(); + bug = 1; + } + } + + for (this.i = 0; this.i < 7; this.i += 1) + { + if (this.ch[this.i] != 'B') + { + if ((this.ch[this.i] == this.ch[(this.i + 1)]) && (this.ch[this.i] == this.ch[(this.i + 2)])) + { + new Board().win(); + bug = 1; + } + this.i += 2; + } + else { + this.i += 2; + } + } + if ((this.ch[4] != 'B') && (( + ((this.ch[0] == this.ch[4]) && (this.ch[4] == this.ch[8])) || ((this.ch[2] == this.ch[4]) && (this.ch[4] == this.ch[6]))))) + { + new Board().win(); + bug = 1; + } + + for (this.i = 0; (this.i < 9) && + (this.ch[this.i] != 'B'); this.i += 1) + { + if (this.i == 8) + { + if (bug == 0) + new Board().draw(); + bug = 0; + } + } + } + + public void mouseReleased(MouseEvent e) + { + System.out.print(""); + } + + public void mouseEntered(MouseEvent e) + { + System.out.print(""); + } + + public void mouseExited(MouseEvent e) { + System.out.print(""); + } + + public void mousePressed(MouseEvent e) { + System.out.print(""); + } + + public static void main(String[] args) + { + new GameApplet(); + } +} + + + + + + + + + + + + +class Board implements WindowListener +{ + public void win() + { + String message = "Congratulations !!!!! \n \nYou win \n"; + + JOptionPane pane = new JOptionPane(message); + JDialog dialog = pane.createDialog(new JFrame(), "Dilaog"); + dialog.show(); + } + + public void draw() + { + String message = "Players the result is !!!!! \n \nSTALEMATE \n"; + + JOptionPane pane = new JOptionPane(message); + JDialog dialog = pane.createDialog(new JFrame(), "Dilaog"); + dialog.show(); + } + + public void windowClosing(WindowEvent we) + { + System.exit(0); + } + + public void windowOpened(WindowEvent we) + { + } + + public void windowClosed(WindowEvent we) + { + } + + public void windowActivated(WindowEvent we) + { + } + + public void windowDeactivated(WindowEvent we) + { + } + + public void windowIconified(WindowEvent we) + { + } + + public void windowDeiconified(WindowEvent we) + { + } +}