Python Connect 4游戏

时间:2015-12-03 01:01:14

标签: python

define('DB_NAME', 'DATABASE');
define('DB_USER', 'USERNAME');
define('DB_PASSWORD', 'PASSWORD');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}



$value = htmlspecialchars($_POST['patient']);
$value4 = htmlspecialchars($_POST['year']);
$value5 = htmlspecialchars($_POST['gender']);
$value6 = htmlspecialchars($_POST['age']);
$value7 = htmlspecialchars($_POST['height']) . '.' . htmlspecialchars($_POST['height_inch']);
$value8 = htmlspecialchars($_POST['weight']);
$value9 = htmlspecialchars($_POST['foot_length']);
$value10 = htmlspecialchars($_POST['sheight']) . '.' . htmlspecialchars($_POST['sheight1']);
$value11 = htmlspecialchars($_POST['Amputation']);

$value13 = htmlspecialchars($_POST['Side']);

$value16 = htmlspecialchars($_POST['Flesh']);

$value18 = htmlspecialchars($_POST['Activity']);

$value21 = htmlspecialchars($_POST['practitioner']);
$value22 = htmlspecialchars($_POST['phone']);
$value23 = htmlspecialchars($_POST['email']);

$value24 = htmlspecialchars($_POST['Account']);
$value25 = htmlspecialchars($_POST['companyname']);
$value26 = htmlspecialchars($_POST['streetaddress']);
$value27 = htmlspecialchars($_POST['city']);
$value28 = htmlspecialchars($_POST['state']);
$value29 = htmlspecialchars($_POST['zip']);
$value30 = htmlspecialchars($_POST['companyname2']);
$value31 = htmlspecialchars($_POST['streetadress2']);
$value32 = htmlspecialchars($_POST['city2']);
$value33 = htmlspecialchars($_POST['state2']);
$value34 = htmlspecialchars($_POST['zip2']);
$value35 = htmlspecialchars($_POST['foot']);

$value39 = htmlspecialchars($_POST['purchaseorder']);
$value40 = htmlspecialchars($_POST['radio']);
$value41 = htmlspecialchars($_POST['lightflesh2']);
$value42 = htmlspecialchars($_POST['darkfleah2']);
$value43 = htmlspecialchars($_POST['foamcalf']);
$value44 = htmlspecialchars($_POST['additional']);
$value45 = htmlspecialchars($_POST['Sock1']);
$value46 = htmlspecialchars($_POST['Sock2']);
$value47 = htmlspecialchars($_POST['Sock3']);
$value48 = htmlspecialchars($_POST['day']);

//$sql = "INSERT INTO order_form (patient, newamputee, yearamputee, year, gender, age, height, weight, foot_length, sheight, ak, bk, left1, right1, bilateral, light_flesh, dark_flesh, k2, k3, k4, k4_extrme, practitioner, email, Account, companyname, streetaddress, city, state, zip, companyname2, streetaddress2, city2, state2, zip2, UltraStride, ActiveStride, NaturalStride, K2_ComfortStride, purchaseorder, radio, lightflesh2, darkfleah2, foamcalf, additional, Sock1, Sock2, Sock3, ground, thirdday, twoday, nextday) VALUES ('$value', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12', '$value13', '$value14', '$value15', '$value16', '$value17', '$value18', '$value19', '$value20', '$value21', '$value22', '$value23', '$value24', '$value25', '$value26', '$value27', '$value28', '$value29', '$value30', '$value31', '$value32', '$value33', '$value34', '$value35', '$value36', '$value37', '$value38', '$value39', '$value40', '$value41', '$value42', '$value43', '$value44', '$value45', '$value46', '$value47', '$value48', '$value49', '$value50', '$value51')";


$update = "UPDATE 'j_users'
SET patient = '$value', year = '$value4', gender = '$value5', age = '$value6', height = '$value7', weight = '$value8', foot_length = '$value9', sheight = '$value10', Amputation = '$value11', Side = '$value13', Flesh = '$value16', Activity = '$value18', practitioner='$value21', phone='$value22', email='$value23', Account = '$value24', companyname = '$value25', streetadress='$value26', city='$value27', state='$value28', zip='$value29', companyname2='$value30', streetadress2='$value31', city2='$value32', state2='$value33', zip2='$value34', foot='$value35', purchaseorder='$value39', radio='$value40', lightflesh2='$value41', darkfleah2='$value42', foamcalf='$value43', foamcalf='$value44', Sock1='$value45', Sock2='$value45', Sock3='$value46', day='$value47'

WHERE user_login = '" . $user . "'";

if (!$update) {
    die('Invalid query: ' . mysql_error());
}

mysql_query($update, $link);

if (!mysql_query($update)) {
die('Error: ' . mysql_error()) ;
mysql_close();
}

我必须创建一个具有给定列和行尺寸的连接4板(现在)。我目前已经创建了主板,但我无法弄清楚如何让COLUMNS = 5 ROWS = 4 def empty_board(): board_rows =["| \t\t |"] * ROWS board_columns = [" ___ "] * COLUMNS for i in range (ROWS): print(board_rows[i]) print("+", end= " " ) for i in range(COLUMNS): print(board_columns[i], end="") print print("+") for i in range(COLUMNS): column_number = i+1 print(" " + str(column_number), end = " ") def column_choice(): player = 1 if player % 2 != 0 : column = input("Player 1, please choose a column to make your next move ") else: column = input("Player 2, please choose a column to make your next move ") player += 1 return column def new_board(column): moves = 0 column = int(column) - 1 if moves % 2 == 0: key = "X" else: key = "O" board_rows = ["| \t\t |"] * ROWS board_columns = [" ___ "] * COLUMNS for i in range (ROWS): print(board_rows[i]) board_rows.pop(column) board_rows = board_rows.insert(column, key) print(board_rows) print("+", end= " " ) for i in range(COLUMNS): print(board_columns[i], end="") print("+") for i in range(COLUMNS): column_number = i+1 print(" " + str(column_number), end = " ") if __name__ == "__main__": print() print("Welcome to Connect 4") print() new_board(column_choice()) X进入正确的位置。例如,现在当我运行程序时,O将进入整个列。如果你能提供任何帮助我会很感激!

2 个答案:

答案 0 :(得分:1)

board_rows =["|   \t\t  |"] * ROWS
board_columns = [" ___ "] * COLUMNS

将列表中的每个条目/项指向相同的内存地址,因此更改一列会更改它们,因为它们都指向同一个位置。

for row in board_rows:
    print id(row)

使用list comprehension或for循环来获取列表中的不同项(内存位置)。

board_rows =["|   \t\t  |" for row in ROWS]
for row in board_rows:
    print id(row)

答案 1 :(得分:0)

老实说,这是一种代表文本板的非常复杂的方式。如果可能的话,我建议使用面向对象的方法,例如将板放入列表列表中,列表中的每个条目都是表示行的字符串列表,使用__repr__方法帮助更容易地将其可视化。这是我写的一个例子。

class Board(object):

def __init__(self, rows, columns):
    self.rows = rows
    self.columns = columns
    self.board = [['_' for i in range(self.columns)] for i in range(self.rows)]
def __repr__(self):
    board = ''
    for row in self.board:
        board += ''.join(row) + '\n'
    return board

       f = Board(5, 5)
=> None
   f
=> _____
   _____
   _____
   _____
   _____

这样,空白板表示为双列表理解,其中rows参数是垂直长度,column参数是水平长度。

使用此代码,尝试编写一种方法,通过找到对手不在的列中的最低点,将棋子添加到棋盘上。