这是我的参考代码:
import math
def tic_tac_toe():
board1 = []
end = False
def choose():
global num
num = input("How large do you want the grid? (input the SIDE length)")
for a in range(num**2):
board1.append(a)
def draw():
x = num
v = 0
for a in range(x):
print
for b in range(x):
print(board1[v]),
v = v + 1
size = num
rows = [tuple(range(i * size, (i + 1) * size)) for i in range(size)]
cols = [tuple(range(i, size ** 2 * 1, size)) for i in range(size)]
diags = [tuple(i * (size + 1) for i in range(size)), tuple(i * (size - 1) for i in range(1, size + 1))]
global howtowin
howtowin = tuple(rows + cols + diags)
def p1():
n = choose_number()
if board1[n] == 'X' or board1[n] == 'O':
print("\nAre you stupid... Try again")
p1()
else:
board1[n] = 'X'
def p2():
n = choose_number()
if board1[n] == 'X' or board1[n] == 'O':
print("\nAre you stupid... Try again")
p2()
else:
board1[n] = 'O'
def choose_number():
while True:
try:
a = int(input())
if a in board1:
return a
else:
print("\nCan you not count to " + str(num**2) + "? Try again")
except ValueError:
print("\nI don't speak French! Try again")
def check_board():
count = 0
for a in howtowin:
if board1[a[0]] == board1[a[1]] == board1[a[2]] == 'X': #check all lengths 3-...
print("\nPlayer 1 Wins!")
print("Congratulations!\n")
return True
if board1[a[0]] == board1[a[1]] == board1[a[2]] == 'O':
print("\nPlayer 2 Wins!")
print("Congratulations!\n")
return True
for a in range(num*num):
if board1[a] == 'X' or board1[a] == 'O':
count += 1
if count == num*num:
print("The game ends in a Tie\n")
return True
choose()
while not end:
draw()
end = check_board()
if end == True:
break
print("\n\nPlayer 1 choose where to place a 'X'")
p1()
print
draw()
end = check_board()
if end == True:
break
print("\n\nPlayer 2 choose where to place a 'O'")
p2()
print
if raw_input("Play again (y/n)\n") == 'y':
print
tic_tac_toe()
else:
print("\nThanks for playing!!!!!!")
tic_tac_toe()
我的问题是check_board();我将其硬编码为3的边值。我现在需要它来处理任何边长值。
答案 0 :(得分:0)
我发现这是一个非常优雅的解决方案,使用all
和any
:
def is_winning_row(board, size, player):
return any((all(board[i][j]==player for j in range(size))) for i in range(size))
如果True
player
上有board
行,False
,则此函数将返回size
。
从这里,您可能会发现如何使用列,然后使用对角线。
如果您不想将size
传递给该函数,只需将len(board)
内的board
替换为import java.util.Scanner;
class binary_diff
{
public String diff(String st1,String st2)
{
String nst="",max="";char b='0';boolean tf=(st1.length()>=st2.length());
int l1=st1.length(),l2=st2.length();
if(l1<l2)
for(int a=1;a<=l2-l1;a++)
st1='0'+st1;
else if(l2<l1)
for(int a=1;a<=l1-l2;a++)
st2="0"+st2;
if(!tf)for(int a=l1-1;a>=0;a--)
if(st1.charAt(a)!=st2.charAt(a))
if(st2.charAt(a)=='1'){max=st2;st2=st1;st1=max;break;}
for(int a=st1.length()-1;a>=0;a--)
{
if(st1.charAt(a)=='1' && st2.charAt(a)=='0')
{
if(b=='1')
{nst='0'+nst;b='0';}
else
nst='1'+nst;
}
else if(st1.charAt(a)==st2.charAt(a) && st2.charAt(a)=='1')
{
if(b=='1')
{nst='1'+nst;b='1';}
else
nst='0'+nst;
}
else if(st1.charAt(a)=='0' && st2.charAt(a)=='1')
{
if(b=='1')
nst='0'+nst;
else
{nst='1'+nst;b='1';}
}
else
{
if(b=='1')
nst='1'+nst;
else
nst='0'+nst;
}
}
return nst;
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the two numbers");
String s1=sc.next();
String s2=sc.next();
binary_diff bd=new binary_diff();
System.out.println(bd.diff(s1,s2));
}
}
(只要shiva
goodshiva
goodshiva
goodshivagood
为{}一个正方形)。