猜拳遊戲 最高效版本

2021-07-04 19:48:03 字數 1801 閱讀 7557

/*概述:玩家和電腦出拳,贏方加分,輸的一方不加分。每一局之後詢問玩家是否繼續。

分析:物件1:玩家,同電腦。方法:被詢問時選擇出拳並輸出選擇(列舉)

物件2:電腦,屬性:姓名,出的拳頭,分數。方法:出拳,判斷輸贏(傳玩家物件)並計分。

每結束一局,進行詢問。

電腦設定成玩家的繼承,方便。

add:名字和分數在建立物件同時要初始化。自定義構造方法即可。用alloc,init。加方法響應。

ps:@propert生成的屬性雖然是私有的,可以被子類繼承,不能直接訪問。但是可以通過父類的getter和setter方法 間接訪問,如super.name,實質就是呼叫父類的方法進行間接訪問和修改。

*/#import #import "bot.h"

int main(int argc, const char * argv)

[b1 fistautoandjudgewith:player];

nslog(@"是否繼續猜拳:y-繼續,n-結束");

rewind(stdin);

scanf("%c",&ifcontinue);

}while (ifcontinue != 'n' || ifcontinue != 'n');

nslog(@"see you!");

}return 0;}//

//  person.h

//  150819-oc2

#import //typedef enum fistselect;

@inte***ce person : nsobject

@property short  fist;

@property nsstring* name;

@property short score;

- (void)fistmethodofperson;

- (instancetype)initwithname:(nsstring*)name andscore:(short)score;

@end

////  person.m

//  150819-oc2

#import "person.h"

@implementation person

- (void)fistmethodofperson

}while (_fist < 1 || _fist > 3);

}- (instancetype)initwithname:(nsstring *)name andscore:(short)score

return self;

}@end

////  bot.h

#import "person.h"

@inte***ce bot : person

- (void)fistautoandjudgewith:(person*)player;

@end

////  bot.m

#import "bot.h"

#import @implementation bot

- (void)fistautoandjudgewith:(person *)player

else if(super.fist == player.fist)

else

nslog(@"比分為%@:%hd------%@:%hd",player.name, player.score, super.name,super.score);

猜拳遊戲Java

拿到這道問題,大值瀏覽下需要的類,既然是遊戲,game類中包含了init 與begin 兩個方法,可以猜測,game類作為了乙個 控制器 的角色,在裡面進行操作,建立person與computer類的物件,並呼叫各showquan 方法,對結果進行比較,得出是否獲勝的結論。思路清晰後,開始編寫相關的...

python 猜拳遊戲

ubuntu下編寫的乙個小遊戲,強迫症患者表示玩了好久。the finger guessing game coding utf 8 匯入乙個包,可以理解為乙個工具箱 import random 等待玩家輸入 playerpls raw input 請輸入 剪刀 0 石頭 1 布 2 player i...

if應用 猜拳遊戲

猜拳遊戲的動作包括。玩家出拳,電腦出拳,判斷結果,反饋。玩家出拳 player int input 請出拳 剪刀 0 石頭 1 布 2 剪刀是0,石頭是1,布是2 print 玩家出拳 d player 通過簡單的數學來代替實際的出拳,從鍵盤中獲取玩家出拳資訊,並轉化為整數量。注釋掉的 是用來檢測這...