Chess

From Computer Science Wiki
This a problem set for you to work through [1]

This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are to HELP YOU THINK THROUGH problems. The solution is at the bottom of this page, but please don't look at it until you have tried (and failed) at least three or four times.

The Problem[edit]

This problem is designed to test your skill and knowledge of programming in python.


Some Code to Get You Started[edit]

# chess
board = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64]

def draw_board():
    row = 0
    for i in range(0,64):
        piece = ("[ " + str(board[i]).zfill(2) + " ]")
        print piece.center(6),    
        row = row + 1
        if row == 8:
            print ("\n")
            row = 0 
    return

def initialize():
    # black team
    for i in range(8,16):
        board[i] = "b.p" + str(i-7) + ""
    board[0] = "b.r1"    
    board[7] = "b.r2" 
    board[6] = "b.n1"
    board[1] = "b.n2"
    board[2] = "b.b1"
    board[5] = "b.b2"
    board[3] = "b.Qu"
    board[4] = "b.Ki" 
 
     # white team
    for i in range(48,57):
        board[i] = "w.p" + str(i-48) + ""
    board[56] = "w.r1"    
    board[63] = "w.r2" 
    board[57] = "w.n1"
    board[62] = "w.n2"
    board[61] = "w.b1"
    board[58] = "w.b2"
    board[60] = "w.Qu"
    board[59] = "w.Ki"    
       
             
    return

def move(piece,destination):
    # is piece valid?
    # is destination valid (basic int 1 to 64)
    # is the move valid for that piece?
    # would the move place the king in check?
    return

initialize() 
draw_board() 
player_move = raw_input("What is your move your command should be piece and then square you want to move to. For example, p2 to 18. > ")
piece = player_move[0:2]
destination = player_move[-2:]
move(piece,destination)

Take This Farther[edit]

  • If you manage to create a program to make valid chess moves, start builing a function that makes INTELLIGENT moves.

How you will be assessed[edit]

Every problem set is a formative assignment. Please click here to see how you will be graded

References[edit]