十点半


*十点半

题目描述

       十点半是一个纸牌游戏,或者说数字游戏。这里简化一下,规则是每个人摸两张牌,然后只通过加减运算,如果能够得到十点半的话就算赢,否则就输。扑克从2到K分别代表2~13点,A代表半点,然后王或老头或司令随便你怎么叫,不分大小,都代表半点。

输入

输入有多组数据。第一行一个正整数T代表数据的组数。接下来N行,每行两张牌。其中11到13的牌是J,Q,K,王是S。

输出

       输出也要N行,每行的格式是如果赢了Case P: WIN,输了Case P: LOSE。其中P代表是第几组数据。

示例输入

4
10 A
A J
10 S
2 8

示例输出

Case 1: WIN
Case 2: WIN
Case 3: WIN
Case 4: LOSE
 
 
源码:
#include <stdio.h>

float Analy(char a[]);//对字符串进行解析
void Resolve(char a[],char b[],char c[]);//将字符串分解成两部分

int main()
{
	char arr1[10];
	char arr2[10],arr3[10];//存储分解后的字符
	int n,i;
	float a,b;//存储点数
	float t;//用于交换数值

	scanf("%d",&n);
	char c=getchar();
	for(i=0; i<n; i++)
	{
		gets(arr1);
		Resolve(arr1,arr2,arr3);
		a = Analy(arr2);
		b = Analy(arr3);
		if(a<b)
		{
			t=a;
			a=b;
			b=t;
		}
		if(a+b == 10.5 || a*b == 10.5 || a/b == 10.5 || a-b == 10.5)
			printf("Case %d: WIN\n",i+1);
		else
			printf("Case %d: LOSE\n",i+1);
	}
	
	return 0;
}

void Resolve(char a[],char b[],char c[])
{
	int i=0,j=0,k=0;
	
	while(a[i] != ' ')
		b[j++] = a[i++];
	b[j] = '\0';

	i++;
	
	while(a[i] != '\0')
		c[k++] = a[i++];
	c[k] = '\0';
}

float Analy(char a[])
{
	if(a[0] == 'A' || a[0] == 'S')
		return 0.5;
	else if(a[0] == 'J')
		return 11;
	else if(a[0] == 'Q')
		return 12;
	else if(a[0] == 'K')
		return 13;
	else if(a[1] >= '0')//两位数字
		return ((a[0]-'0')*10 + (a[1]-'0'));
	else				//一位数字
		return (a[0]-'0');
}


 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的十点游戏的Python代码,使用Tkinter库进行图形化界面开发。 首先,我们需要导入必要的库和模块: ```python import random import tkinter as tk from tkinter import messagebox ``` 然后,我们需要定义一些常量和变量: ```python CARD_VALUES = {'A': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 0.5, 'J': 0.5, 'Q': 0.5, 'K': 0.5} CARD_SUITES = ['Hearts', 'Diamonds', 'Clubs', 'Spades'] CARD_RANKS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] DEALER_LIMIT = 8 DEALER_STAND = 10 deck = [] player_cards = [] dealer_cards = [] player_score = 0 dealer_score = 0 game_over = False ``` 在这里,我们定义了一些常量和变量。CARD_VALUES是一个字典,它将每个卡的值映射到一个数字。CARD_SUITES是一组花色,CARD_RANKS是一组排名。DEALER_LIMIT是一个常量,它指定了经销商必须抽取更多的卡片的限制。DEALER_STAND是一个常量,它指定了经销商必须停止抽取更多的卡片的限制。DECK是一个列表,它包含所有52张卡片。PLAYER_CARDS是一个列表,它包含玩家的卡片。DEALER_CARDS是一个列表,它包含经销商的卡片。PLAYER_SCORE和DEALER_SCORE是玩家和经销商的分数。GAME_OVER是一个标志,指示游戏是否结束。 接下来,我们需要定义一些函数: ```python def create_deck(): """ Creates a deck of cards. """ global deck deck = [(rank, suite) for rank in CARD_RANKS for suite in CARD_SUITES] random.shuffle(deck) def deal_cards(): """ Deals two cards to the player and two cards to the dealer. """ global player_cards, dealer_cards, deck player_cards = [deck.pop(), deck.pop()] dealer_cards = [deck.pop(), deck.pop()] def get_card_value(card): """ Returns the value of a card. """ rank, _ = card return CARD_VALUES[rank] def get_hand_score(hand): """ Calculates the score of a hand. """ score = sum([get_card_value(card) for card in hand]) if score <= 10 and any(card[0] == 'A' for card in hand): score += 10 return score def player_hit(): """ Draws a card for the player. """ global player_cards, player_score, game_over if game_over: return player_cards.append(deck.pop()) player_score = get_hand_score(player_cards) if player_score > 10.5: game_over = True messagebox.showinfo('Game Over', 'You Lose!') def dealer_hit(): """ Draws a card for the dealer. """ global dealer_cards, dealer_score, game_over if game_over: return dealer_score = get_hand_score(dealer_cards) if dealer_score >= DEALER_STAND: game_over = True elif dealer_score >= DEALER_LIMIT: dealer_cards.append(deck.pop()) dealer_score = get_hand_score(dealer_cards) if dealer_score >= DEALER_STAND: game_over = True def check_game_over(): """ Checks if the game is over. """ global game_over if game_over: return True if len(deck) == 0: game_over = True if player_score > dealer_score: messagebox.showinfo('Game Over', 'You Win!') elif player_score == dealer_score: messagebox.showinfo('Game Over', 'Tie!') else: messagebox.showinfo('Game Over', 'You Lose!') return True return False ``` 在这里,我们定义了一些函数。CREATE_DECK函数创建一个新的卡组,DEAL_CARDS函数将两张卡片分配给玩家和经销商,GET_CARD_VALUE函数返回卡片的值,GET_HAND_SCORE函数计算手的分数,PLAYER_HIT函数让玩家抽取一张卡片,DEALER_HIT函数让经销商抽取一张卡片,CHECK_GAME_OVER函数检查游戏是否结束。 接下来,我们需要定义一个GUI: ```python def create_gui(): """ Creates the GUI for the game. """ global player_score, dealer_score, game_over root = tk.Tk() root.title('Ten and a Half') # Player cards player_card_frame = tk.Frame(root) player_card_frame.pack(side=tk.TOP, padx=10, pady=10) tk.Label(player_card_frame, text='Your Cards:').pack(side=tk.LEFT) tk.Label(player_card_frame, textvariable=player_score).pack(side=tk.RIGHT) player_card_listbox = tk.Listbox(player_card_frame, height=2) player_card_listbox.pack(side=tk.TOP) for card in player_cards: player_card_listbox.insert(tk.END, f'{card[0]} of {card[1]}') # Dealer cards dealer_card_frame = tk.Frame(root) dealer_card_frame.pack(side=tk.TOP, padx=10, pady=10) tk.Label(dealer_card_frame, text='Dealer Cards:').pack(side=tk.LEFT) tk.Label(dealer_card_frame, textvariable=dealer_score).pack(side=tk.RIGHT) dealer_card_listbox = tk.Listbox(dealer_card_frame, height=2) dealer_card_listbox.pack(side=tk.TOP) for card in dealer_cards: dealer_card_listbox.insert(tk.END, f'{card[0]} of {card[1]}') # Buttons button_frame = tk.Frame(root) button_frame.pack(side=tk.TOP, padx=10, pady=10) tk.Button(button_frame, text='Hit', command=player_hit).pack(side=tk.LEFT) tk.Button(button_frame, text='Stand', command=dealer_hit).pack(side=tk.LEFT) tk.Button(button_frame, text='Quit', command=root.quit).pack(side=tk.LEFT) while not check_game_over(): root.update() root.mainloop() ``` 在这里,我们定义了一个CREATE_GUI函数,它创建了一个GUI。我们创建了一个窗口,并将玩家卡片和经销商卡片分别放置在两个框中。我们还创建了三个按钮:HIT,STAND和QUIT。 最后,我们需要执行这些函数: ```python if __name__ == '__main__': create_deck() deal_cards() player_score = get_hand_score(player_cards) dealer_score = get_hand_score(dealer_cards) create_gui() ``` 在这里,我们创建了一个新的卡组,分配了两张卡片给玩家和经销商,并计算了分数。我们还调用了CREATE_GUI函数,以创建游戏的GUI。 这就是一个简单的十点游戏的Python代码,使用Tkinter库进行图形化界面开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值