小猫钓鱼java代码,【Java个人笔记】练习-小猫钓鱼

尚未完成

猫类

package 小猫钓鱼;

import java.util.Random;

public class Cat {

private String name;

private int[] card = new int[100];

private int cardnumber = 0;

public Cat(String name, int cardnumber) {

this.name = name;

this.cardnumber = cardnumber;

Random rand = new Random();

for (int i = 0; i < cardnumber; i++) {

this.card[i] = rand.nextInt(9);

}

}

// 打印所有牌

public void show() {

System.out.println(this.name + "的牌是:");

for (int i = 0; i < this.cardnumber; i++) {

System.out.println(this.card[i] + " ");

}

}

// 依次打出手中的牌

public int outcard() {

int temporary = card[0];

for (int i = 0; i < (this.cardnumber - 1); i++) {

card[i] = card[i + 1];

}

this.cardnumber -= 1;

System.out.println(this.name + "打出" + temporary);

return temporary;

}

// 接收桌面上的牌

public void getcard(int[] desktop) {

System.out.println(this.name + "获得桌面上所有牌");

int i = desktop.length;

while ((i - 1) >= 0) {

this.card[cardnumber] = desktop[i - 1];

i--;

cardnumber++;

}

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

// 目前手牌数

public int getCardnumber() {

return cardnumber;

}

public void setCardnumber(int cardnumber) {

this.cardnumber = cardnumber;

}

// 单元测试

// public static void main(String[] args) {

// Cat cat1 = new Cat("咖啡猫", 5);

//

// cat1.show();

// System.out.println(cat1.outcard());

// cat1.show();

// int[] a = new int[5];

// for (int i = 0; i < 5; i++)

// a[i] = i + 1;

// cat1.getcard(a);

// cat1.show();

// System.out.println(cat1.getCardnumber());

// for (int i = 0; i < 5; i++)

// System.out.println(a[i]);

//

// }

}

桌面类

package 小猫钓鱼;

import java.util.ArrayList;

public class Table {

ArrayList tablecard=new ArrayList();

// 获得猫打出的牌

public void getcard(int card) {

tablecard.add(Integer.valueOf(card));

}

// 返回所有牌

public int[] outcard() {

tablecard.trimToSize();

int i=tablecard.size();

int[] a=new int[i];

for(;i>0;i--) {

Integer tem=tablecard.get(i-1);

a[i-1]= tem.intValue();

}

return a;

}

//

public void clean() {

tablecard.clear();

}

}

主程序类

package 小猫钓鱼;

public class Game {

public static void main(String[] args) {

// TODO 自动生成的方法存根

Cat cat1 = new Cat("咖啡猫", 5);

Cat cat2 = new Cat("汤姆", 5);

Table table = new Table();

int b = 10;

int time = 0;

cat1.show();

cat2.show();

while (cat1.getCardnumber() != 0 || cat2.getCardnumber() != 0) {

int a = cat1.outcard();

table.getcard(a);

if (a == b) {

cat1.getcard(table.outcard());

table.clean();

cat1.show();

cat2.show();

}

b = cat2.outcard();

table.getcard(b);

if (a == b) {

cat2.getcard(table.outcard());

table.clean();

cat1.show();

cat2.show();

}

time++;

}

if (cat1.getCardnumber() == 0)

System.out.println(cat2.getName() + "获得最终胜利,共进行了" + time + "轮比赛");

else

System.out.println(cat1.getName() + "获得最终胜利,共进行了" + time + "轮比赛");

}

}

待更新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值