Java小遊戲演算法入門(撲克牌演算法)

2021-08-04 17:57:22 字數 4572 閱讀 4675

一)燜雞簡介

1)規則

略2)案例

略二)演算法設計

1)列舉型別設計

public class menjienum 

public int getvalue()

} /**

*燜雞牌花色型別

* * @author xiu

* @version 2023年8月1日 下午4:07:14

*/public enum cardbigtype

}

2)判斷牌的型別演算法

/**

* 是否是順子

* @param card

* @return

*/public static boolean getsmalltypeisshunzi(int card)

} else if((card[0] == 11 && card[1] == 12 && card[2] == 0) ||

(card[0] == 12 && card[1] == 0 && card[2] == 1) || (card[0] == 2 && card[1] == 3 && card[2] == 1))

return false; }

/*** 是否是豹子

* @param card

* @return

*/public static boolean getsmalltypeisbaozi(int card)else }

/*** 是否是對子

* @param card

* @return

*/public static boolean getsmalltypeisduizi(int card)else }

/*** 是否是特殊

* @param card

* @return

*/public static boolean getsmalltypeisteshu(int card)else }

/*** 是否是同花

* @param cardbigtypes

* @return

*/public static boolean getbigtypeistonghua(cardbigtype cardbigtypes)else }

/*** 獲得玩家牌的型別

* @param ids

* @return

*/public static mjcardtype getcardtype(listids)

for(int i = 0 ; i < ids.size() ; i ++)

sortsamllcard(cardsmalltypes);

boolean isshunzi = getsmalltypeisshunzi(cardsmalltypes);

if(getbigtypeistonghua(cardbigtypes))else

}elseelse if(isshunzi)else if(getsmalltypeisduizi(cardsmalltypes))else if(getsmalltypeisteshu(cardsmalltypes))else

} }/**

* 獲得某張牌的花色

* @param id

* @return

*/public static cardbigtype getbigtype(int id)else if (14 <= id && id <= 26) else if (27 <= id && id <= 39) else if (40 <= id && id <= 52)

return bigtype;

}

3)兩個使用者使用者比牌演算法

先計算出連個玩家牌的型別,根據牌型別比較

/**

* 兩個使用者比牌

* @param cardbigtypelist

* @param ids

*/public static boolean getwhobig(listmenjicardtypelist,listids1,listids2)

sortsamllcard(array1);

sortsamllcard(array2);

int cardtype = new int[7];

mjcardtype winercardtype;

int index;

for (int i = 0; i < menjicardtypelist.size(); i++)

} if(cardtype[0] >= 1)else

} else if(cardtype[1] >= 1) else if(cardtype[2] >= 1) else if(cardtype[3] >= 1) else if(cardtype[4] >= 1) else if(cardtype[5] >= 1)else

if(cardtype[index] == 2)

}break;

case 4:

result = towcardwhobug(array1[1], array2[1]);

if(result == 0)else

if(array2[0] != array2[1])else

result = towcardwhobug(id1, id2);

}break;

default:

result = 0;

break;

}if(result == 1)else

}elseelse

} }

/*** 兩張牌比大小

* @param id1

* @param id2

* @return 0:平牌,1:第一張大,2:第二張大

*/public static int towcardwhobug(int id1,int id2)else if((id1 == 0 || id1 == 1) && (id2 == 0 || id2 == 1))else

}else if(id1 == 0 || id1 == 1 || id2 == 0 || id2 == 1)else

}elseelse

} return result; }

/*** 將使用者的牌排序 2<3<4<5<6<7<8<9<10ids[j])

}if(index != i)

} for(int i = 0; i < ids.length; i++)else if(ids[i] == 14)

} }

4)在一堆牌list裡面能找出最大牌和第二大牌

/**

* 計算出第幾部牌是最大的

* @param cardlists

*/public static int sortcard(list> cardlists)

} if(isteshu)

}}

if(isteshuandbaozi)else

}} return index; }

/**計算出第幾部牌是第二大的

* @param cardlists

* @param maxindex

* @return

*/public static int sortcard2(list> cardlists, int maxindex)

三)測試

public static void main(string args) 

collections.shuffle(initallpai);

//玩家的牌堆,三張乙份

list> cardlists = new arraylist>();

//根據玩家數量,從牌堆中取出n份3張放到cardlists集合中

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

mjcardtype cardtype = getcardtype(cards);

system.out.println("第" + i +"個玩家牌:"+cards +"牌型別為:" +cardtype);

cardlists.add(cards);

}// 最大牌型是第 maxpai+1 部

int maxpai = sortcard(cardlists);

//第二大牌索引

int secondmaxpai = sortcard2(cardlists, maxpai);

system.err.println("第一大" +maxpai);

system.err.println("第二大"+secondmaxpai);

}

測試結果:

第1個玩家牌:[50, 8, 2]牌型別為:san_pai

第2個玩家牌:[1, 34, 16]牌型別為:san_pai

第3個玩家牌:[22, 7, 6]牌型別為:san_pai

第4個玩家牌:[47, 11, 29]牌型別為:san_pai

第一大0

第二大2

C 小遊戲 撲克牌21點

21點撲克牌遊戲 程式說明 該程式是模擬21點撲克牌遊戲,玩家最多可以要5張牌,但是如果牌的點數之和超過21點,則自動出局,在不超過21點的情況下,玩家與莊家比牌的大小,大者為贏家 程式片段分析 標頭檔案處 include include include using namespace std cs...

java實現 簡易撲克牌遊戲

第三季聽課後的綜合實戰,要求如下 1.建立一副撲克牌,不考慮大小王 2.建立兩名玩家,玩家至少要有id 姓名 手牌等屬性,手牌為撲克牌的集合 3.洗牌,將之前建立的撲克牌順序打亂 4.發牌,將洗牌之後的撲克牌集合,從第一張開始,發給兩名玩家,按照一人一張的方式,每人發兩張 5.開始遊戲,比大小,大小...

轉。。。。。撲克牌遊戲

using system using system.collections.generic using system.componentmodel using system.data using system.drawing using system.text using system.window...