Android學習-簡單的猜拳遊戲

今天是2016728java

 

Java製做簡單的猜拳遊戲,dom

 

遊戲界面ide

 

簡單分析:this

 

猜拳比賽須要兩個選手參加,一個是本身,一個是敵人,每一個選手都有兩個屬性,分別是分數和角色;哪一方獲勝,該方的分數自動加1,平局都不加,並記錄平局數,每比完一次賽,計算結果並輸出,而後詢問是否繼續玩,若是繼續,則重複以上動做,若是不想繼續玩,則計算最終結果並輸出退出.spa

 

Person.java 遊戲

package person;get

 

public  class Person {input

 

int score;it

String role;io

 

public  int attack(){return 0;};

 

public int getScore() {

return score;

}

 

public void setScore(int score) {

this.score = score;

}

 

public String getRole() {

return role;

}

 

public void setRole(String role) {

this.role = role;

}

 

public void addScore() {

 

this.score++;

}

 

}

 

Player.java

 

package person;

 

import java.util.Scanner;

 

public class Playerextends Person {

 

public Player() {

// TODO Auto-generated constructor stub

}

 

@Override

public int attack() {

// TODO Auto-generated method stub

 

System.out.println("請出拳:1.石頭2.剪子3.2");

Scanner input = new Scanner(System.in);

 

int index = input.nextInt();

 

return index;

}

 

@Override

public

void addScore() {

super.addScore();

System.out.println("結果:運氣真好,你贏了!");

}

 

}

Enemy.java

package person;

 

import java.util.Random;

 

public class Enemyextends Person {

 

public Enemy() {

// TODO Auto-generated constructor stub

}

 

@Override

public int attack() {

// TODO Auto-generated method stub

 

Random random = new Random();

return random.nextInt(2) + 1;

}

 

@Override

public

void addScore() {

super.addScore();

System.out.println("結果:啊!你輸了!");

}

 

}

Game.java

 

package gameTest;

 

import java.util.Scanner;

 

import person.Enemy;

import person.Player;

 

public class Game {

 

private Player player;

private Enemy enemy;

private String roles[];

private String enemyRoles[];

private String attacks[];

private int peace = 0;

final int stone = 1;

final int cut = 2;

final int cloth = 3;

 

public Game() {

// TODO Auto-generated constructor stub

player = new Player();

enemy = new Enemy();

roles = new String[] { "null", "沸羊羊", "暖羊羊", "美羊羊" };

enemyRoles = new String[] { "null", "喜羊羊", "慢羊羊", "懶羊羊" };

attacks = new String[] { "null", "石頭", "剪子", "" };

}

 

private void setRole() {

Scanner input = new Scanner(System.in);

System.out.print("\t\t請選擇你的角色(1.沸羊羊  2.暖羊羊  3.美羊羊):");

player.setRole(roles[input.nextInt()]);

 

System.out.print("\t\t請選擇對手角色(1.喜羊羊  2.慢羊羊 3.懶羊羊):");

enemy.setRole(enemyRoles[input.nextInt()]);

 

}

 

public void start() {

System.out.println("-----------------歡迎進入羊村遊戲世界--------------------");

System.out.println("\t\t******************");

System.out.println("\t\t**  遊戲開始  **");

System.out.println("\t\t******************");

System.out.println("\t\t出拳規則:1.石頭2.剪子3.");

 

this.setRole();

 

System.out.print("開始遊戲嗎? (y/n)");

Scanner input = new Scanner(System.in);

 

if (input.next().equals("y"))

this.play();

else

System.out.println("退出遊戲");

 

input.close();

 

}

 

public void play() {

int enemyAttack;

while (true) {

System.out.print("請出拳:1.石頭2.剪子3.");

Scanner input = new Scanner(System.in);

 

switch (input.nextInt()) {

case stone:

System.out.println("你出拳:" + attacks[stone]);

enemyAttack = enemy.attack();

System.out.println(enemy.getRole() + "出拳:" + attacks[enemyAttack]);

 

if (enemyAttack == this.cut)

player.addScore();

else if (enemyAttack == this.cloth)

enemy.addScore();

else if (enemyAttack == this.stone) {

peace++;

System.out.println("平局");

}

 

break;

 

case cut:

 

System.out.println("你出拳:" + attacks[cut]);

enemyAttack = enemy.attack();

System.out.println(enemy.getRole() + "出拳:" + attacks[enemyAttack]);

 

if (enemyAttack == this.cloth)

player.addScore();

else if (enemyAttack == this.stone)

enemy.addScore();

else if (enemyAttack == this.cut) {

peace++;

System.out.println("平局");

}

break;

 

case cloth:

System.out.println("你出拳:" + attacks[cloth]);

enemyAttack = enemy.attack();

System.out.println(enemy.getRole() + "出拳:" + attacks[enemyAttack]);

 

if (enemyAttack == this.stone)

player.addScore();

else if (enemyAttack == this.cut)

enemy.addScore();

else if (enemyAttack == this.cloth) {

peace++;

System.out.println("平局");

}

break;

 

default:

break;

 

}

System.out.print("是否開始下一輪?  (y/n)");

 

if (input.next().equals("n")) {

this.Over();

break;

}

System.out.println("--------------------------------");

 

}

}

 

public void Over() {

System.out.println(player.getRole() + "\t對決\t" + enemy.getRole());

int total = player.getScore() + enemy.getScore() + peace;

System.out.println("次數 :" + total);

System.out.println("平局 :" + peace);

System.out.println(player.getRole() + " :" + player.getScore());

System.out.println(enemy.getRole() + " :" + enemy.getScore());

 

if (player.getScore() > enemy.getScore())

System.out.println("最終結果 :" + player.getRole() + "獲勝");

else if (player.getScore() < enemy.getScore())

System.out.println("最終結果 :" + enemy.getRole() + "獲勝");

else

System.out.println("最終結果 :");

System.out.println("--------------------------------");

 

}

 

}

 

Test.java

 

package gameTest;

 

 

public class Test {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

Game game = new Game();

game.start();

}

 

}

 

 

 

二 用swing作的登陸界面

 

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTextField;

 

 

public class Login extends JFrame {

 

/**

 * @param args

 */

public Login()

{

setLayout(null);

JLabel l1 = new JLabel("用戶名:");

JTextField t1 =new JTextField();

JLabel l2 = new JLabel("密碼:");

JTextField t2 =new JTextField();

JButton b1 = new JButton("登陸");

JButton b2 = new JButton("重置");

l1.setBounds(30, 10,50,20);

t1.setBounds(100, 10,150,20);

l2.setBounds(30, 50,50,20);

t2.setBounds(100, 50,150,20);

b1.setBounds(40,80,80,20);

b2.setBounds(140,80,80,20);

add(l1);

add(t1);

add(l2);

add(t2);

 

add(b1);

add(b2);

}

public static void run(JFrame f,final int width,final int height)

{

f.setTitle(f.getClass().getName());

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setSize(width,height);

f.setVisible(true);

}

public static void main(String[] args) {

// TODO Auto-generated method stub

 

run(new Login(),300,400);

}

 

}

相關文章
相關標籤/搜索