Python(四):数字连珠2
对上次的代码作了一些修改。在码的过程中发现,最核心的部分是在横向、竖向和两个对角方向上找到5个以上相同的数字。
自己的思路是将x行y列所在的x行、y列,以及以此为交叉点的两点对角线上的数字,转化成字符串(这部分是程序中get4str()的功能),然后利用字符串相关函数进行处理(待完成)。
最新修改代码如下:
import random # for random.randrange()
import os # for input()
import string # for string.count() ballColorNum = 7 # 7 colors
RowNum = 6 # chesspad have 10 rows
ColNum = 10 # chesspad have 10 cols chPad = [] # save chesspad state
Scores = 0 # save scores class ball():
def __init__(self):
self.color = random.randrange(1,ballColorNum)
self.x = random.randrange(0,RowNum)
self.y = random.randrange(0,ColNum) #---------------------------------------
# initial chesspad state, all 0, COL*ROW
#---------------------------------------
def chesspad_init():
chPad = [[0 for x in range(ColNum)] for x in range(RowNum)]
return chPad
#---------------------------------------
# update chesspad with a ball
#---------------------------------------
def chesspad_update(chPad, ball):
chPad[ball.x][ball.y] = ball.color
return chPad
#---------------------------------------
# redraw chesspad
#---------------------------------------
def chesspad_flush(chPad):
for i in range(len(chPad)):
for c in chPad[i]:
print('%d'%c,end=' ') #for win
#print('\33[1;%dm%d'%(30+c,c),end=' ') #for linux
print('\n')
#---------------------------------------
# count the number of unused spaces in chesspad
#---------------------------------------
def countNull(chPad):
n = 0
for i in range(len(chPad)):
for j in range(len(chPad[i])):
if chPad[i][j] == 0:
n += 1
return n
#---------------------------------------
# move from x1,y1 to x2,y2
#---------------------------------------
def move(chpad,x1,y1,x2,y2):
if x1 > RowNum-1 or x2 > RowNum-1 \
or y2 > ColNum-1 or y1 > ColNum-1:
print('input error') else:
chpad[x1][y1],chpad[x2][y2] = \
chpad[x2][y2],chpad[x1][y1] #---------------------------------------
# calculate_Score
#---------------------------------------
def calculate_Score(chpad):
pass #---------------------------------------
# find
#---------------------------------------
def compare_str(str4dict,x,y):
pass
#---------------------------------------
# generate 4 strings and return a dict:
# FORMART:
# {'RW':'...','CL':'...',
# 'LR':'...','RL':'...'}
# compare with chpad[x][y] which aspears
# 5 times
#---------------------------------------
def get4str(chpad,x,y): sx=sy=slr=srl='' # ROW(X)
for c in chpad[x]:
sx += str(c) # COL(Y)
for i in range(RowNum):
sy += str(chpad[i][y]) # from left-top to right-buttom
for i in range(x):
if x-i <= 0 or y-i <= 0:
break
else:
slr += str(chpad[x-i-1][y-i-1])
slr[::-1] # reverse for i in range(RowNum-x):
if x+i >= RowNum or y+i >= ColNum:
break
else:
slr += str(chpad[x+i][y+i]) # from right-top to left-buttom
for i in range(1,x):
if x-i <= 0 or y+i >= ColNum:
break
else:
srl += str(chpad[x-i][y+i])
srl[::-1] # reverse for i in range(RowNum-x):
if x+i > RowNum-1 or y-i < 0:
break
else:
srl += str(chpad[x+i][y-i]) return {'RW':sx,'CL':sy,'LR':slr,'RL':srl} def main():
print('\n-------------------GAME------------------\n')
pad = chesspad_init() while 1:
n = 1
while 1:
b = ball()
if pad[b.x][b.y] == 0:
print('(%d,%d:%d)'%(b.x, b.y, b.color),end=' ')
pad = chesspad_update(pad, b)
if countNull(pad) == 0:
print("\n\nGAME OVER!")
exit()
n += 1
if n > 3:
break
print('\n') chesspad_flush(pad) x1=y1=x2=y2=0
a = input('Move (x1,y2) to (x2,y2):').split(' ')
x1,y1,x2,y2 = int(a[0]),int(a[1]),int(a[2]),int(a[3])
move(pad,x1,y1,x2,y2 )
print(get4str(pad, x2, y2)) chesspad_flush(pad) if __name__=='__main__':
main()
以上代码后面有修改,98行、112行本来是自己写的一个反转字符串的函数实现,后来才知道用切片str[::-1]实现更简洁。
Python(四):数字连珠2的更多相关文章
- Python 1基础语法四(数字类型、输入输出汇总和命令行参数)
一.数字(Number)类型 python中数字有四种类型:整数.布尔型.浮点数和复数. int (整数), 如 1, 只有一种整数类型 int,表示为长整型,没有 python2 中的 Long. ...
- Python 四种数值类型(int,long,float,complex)区别及转换
Python支持四种不同的数值类型,包括int(整数)long(长整数)float(浮点实际值)complex (复数), 数字数据类型存储数值.他们是不可改变的数据类型,这意味着改变数字数据类型的结 ...
- python 四种数值类型(int,long,float,complex)介绍
Python支持四种不同的数值类型,包括int(整数)long(长整数)float(浮点实际值)complex (复数),本文章向码农介绍python 四种数值类型,需要的朋友可以参考一下. 数字数据 ...
- Python Number(数字) Ⅰ
Python Number(数字) Python Number 数据类型http://www.xuanhe.net/用于存储数值. 数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的 ...
- 如何使用纯 CSS 制作四子连珠游戏
序言:你是否想过单纯使用 CSS 也可以制作一款游戏?甚至可以双人对决!这是一篇非常有趣的文章,作者详细讲解了使用纯 CSS 制作四子连珠游戏的思路以及使用奇淫巧技解决困难问题的方法.因为案例本身比较 ...
- 【笔记】基于Python的数字图像处理
[博客导航] [Python相关] 前言 基于Python的数字图像处理,离不开相关处理的第三方库函数.搜索网络资源,列出如下资源链接. Python图像处理库到底用哪家 python计算机视觉编程— ...
- python基础——数字&集合&布尔类型
Python的核心数据类型 内置对象 对象类型 例子 数字 123,3.1415,3+4j,Decimal(小数),Fraction(分数) 字符串 'dodo',"guido's" ...
- 零基础学习 Python 之数字与运算
写在之前 大家好,这里是零基础学习 Python 系列,在这里我将从最基本的 Python 写起,然后再慢慢涉及到高阶以及具体应用方面.我是完全自学的 Python,所以很是明白自学对于一个人的考验, ...
- Python之数字
Python之数字 int(数字)===>在Python3中,int没有范围,在Python2中,int超出范围就叫长整型(Long). 浮点运算:单精度 float 双精度 double a: ...
- python取数字、字母
python取数字.字母 有一串字符串“lxa7YzU”,其中有大写字母.小写字母和数字,现编写一脚本使得实现以下功能: 将这串字符串中的数字.大写字母.小写字母分别取出来并进行分类. 脚本如下所示: ...
随机推荐
- C 语言 联合union初见
1.什么是联合? “联合”是一种构造类型的数据结构.在一个“联合”内可以定义多种不同的数据类型, 一个被说明为该“联合”类型的变量中,允许装入该“联合”所定义的任何一种数据,这些数据共享同一段内存,已 ...
- Python新手学习基础之条件语句——elif语句
elif语句 (相当于C语言的else if) 在Python中,当我们需要有更多的判断条件时,我们往往会使用另外一种语法表达,即使用elif: if 判断条件1: 执行语句1 elif 判断条件2: ...
- GetMemory 函数解析
GetMemory函数 代码1: void GetMemory(char *p){ p = (char*)malloc(100);}int main(int argc, char *argv[]){ ...
- ural 1017. Staircases
http://acm.timus.ru/problem.aspx?space=1&num=1017 #include <cstdio> #include <cstring&g ...
- python编程之处理GB级的大型文件
一般我们采取分块处理,一次处理固定大小的块. def read_in_chunks(file_obj,chunk_size): """Lazy function (gen ...
- 利用php unpack读取c struct的二进制数据,struct内存对齐引起的一些问题
c语言代码 #include <stdio.h> struct test{ int a; unsigned char b; int c; }; int main(){ FILE *fp; ...
- 转:使用linq to sql 随机取一行数据的方法
原文地址:http://outofmemory.cn/code-snippet/1760/usage-linq-to-sql-suiji-take-yixing-data-method 虽然这看来已经 ...
- Spring中给Bean注入集合
Spring中如果一个Bean里含有集合元素,需要给Bean里的集合元素注入元素时,可以采用如下方法,一个是构造器注入,一个是setter注入 JavaBean源代码: import java.uti ...
- C语言中所有变量和常量所使用的内存总结
(1)相同点:三种获取内存的方法,都可以给程序提供可用内存,都可以用来定义变量给程序用.(2)不同点:栈内存对应C中的普通局部变量(别的变量还用不了栈,而且栈是自动的,由编译器和运行时环境共同来提供服 ...
- Android 体系结构
Anroid是在Linux基础开发出的一个移动设备开发平台.它自上而下包含四个部分: Application(应用程序) Applicaton Framework(应用程序框架) Libraries& ...