'how to check if the 2d array contain the requirement, and if yes, return the toal score in python
I have a 5 * 5 two-dimensional array, and a dice will be thrown 25 times. For each throw, the user needs to put the value into the two-dimensional array. Because this is 5 * 5, the computer needs to identify what standards you meet after filling the last 25 grids. The computer calculates the value set by each standard, and then adds up the values to tell the user how many points you get in this round, such as if there is five same numbers, a full house, and so on. I will show the scores of each standard in the form of a dictionary. My question is, how can I let the computer identify and return the total score after a round? At present, I have written out each standard and can put the number into the corresponding position, but I don't know how to let the computer know which standards users meet and calculate the total value.
from random import randint
from collections import Counter
cols,rows=5,5
arr = [[0]*cols for j in range(rows)] #create a two dimensional array
rules = {
"five": 80,
"four": 40,
"three": 20,
"two": 5,
"checkConsecutive": 50,
"three_two": 30,
"Two_two_one": 10,
}
#rules for each of the standard
def position(x, y) -> None:
x_value = ["A", "B", "C", "D", "E"]
y_value = ["1", "2", "3", "4", "5"]
while arr[y_value.index(y)][x_value.index(x)] != 0:
x = input("Space occupied, Choose a new X value:")
y = input("Space occupied, Choose a new Y value:")
arr[y_value.index(y)][x_value.index(x)] = r
#if the user input to the same postion, it will say choose a new position
return arr
#input the die value into the board
def five(S5n:list):
#check if there is a five
return len(set(S5n))==1
def four(S4n:list):
#check if there is a four
return 4 in list(dict(Counter(S4n)).values()) and 1 in list(dict(Counter(S4n))) and len(list(dict(Counter(S4n)).values()))==2
def three(S3n:list):
#check if there is a three
return 3 in list(dict(Counter(S3n)).values()) and ((1 in list(dict(Counter(S3n)).values()) and 1 in list(dict(Counter(S3n)).values())))and len(list(dict(Counter(S3n)).values()))==3
def Two_two_one(S221n:list):
#check if there is a 2+2+1
return 2 in list(dict(Counter(S221n)).values()) and 2 in list(dict(Counter(S221n))) and 1 in list(dict(Counter(S221n)))and len(list(dict(Counter(S221n)).values()))==3
def three_two(S3_2n:list):
#check if there is a 3+2
return 3 in list(dict(Counter(S3_2n)).values()) and 2 in list(dict(Counter(S3_2n)).values()) and len(list(dict(Counter(S3_2n)).values()))==2
def checkConsecutive(l):
#check if there is a consecutive
return sorted(l) == list(range(min(l), max(l)+1)) and len(l)==5
def two(two:list):
#check if there is only one group of two identical number
return 2 in list(dict(Counter(two)).values()) and (1 in list(dict(Counter(two)).values())*3) and len(list(dict(Counter(two)).values()))==4
def Score_collections():
points=0
name=" "
for i in[five,four,three,two,checkConsecutive,three_two,Two_two_one]:
def show():
for i in arr:
print(i)
#showing the board
for i in range(25):
throw = 0
throw+=1
r=randint(1, 6)
show()
print(f"this is what you got: {r}")
x= input("X value:")
y = input("Y value:")
position(x,y)
#call the function and repeated for 25 times and also shows the die number
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
