Chess: Difference between revisions

From Computer Science Wiki
 
(5 intermediate revisions by 2 users not shown)
Line 5: Line 5:
== The Problem ==
== The Problem ==


This problem is designed to test your skill and knowledge of programming in python.
This problem is designed to test your skill and knowledge of programming in python. It is also designed to test your computational thinking.
 


== Some Code to Get You Started ==
== Some Code to Get You Started ==
Line 28: Line 27:
     # black team
     # black team
     for i in range(8,16):
     for i in range(8,16):
         board[i] = "p" + str(i-7) + ""
         board[i] = "b.p" + str(i-7) + ""
     board[0] = "r1"     
     board[0] = "b.r1"     
     board[7] = "r2"  
     board[7] = "b.r2"  
     board[6] = "n1"
     board[6] = "b.n1"
     board[1] = "n2"
     board[1] = "b.n2"
     board[2] = "b1"
     board[2] = "b.b1"
     board[5] = "b2"
     board[5] = "b.b2"
     board[3] = "Qu"
     board[3] = "b.Qu"
     board[4] = "Ki"  
     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
     return


Line 52: Line 64:
piece = player_move[0:2]
piece = player_move[0:2]
destination = player_move[-2:]
destination = player_move[-2:]
move(piece,destination)  
move(piece,destination)
</syntaxhighlight>
</syntaxhighlight>


Line 67: Line 79:
<references />
<references />


== One Possible Solution ==
<div class="toccolours mw-collapsible mw-collapsed">
Click the expand link to see one possible solution, but NOT before you have tried and failed!
<div class="mw-collapsible-content">
<syntaxhighlight lang="python" line="1" >
#
# this function returns a HTML opening and closing pair of tag
#
def make_tags(tag, word):
    print("<" + tag + ">" + word + "</" + tag + ">")
    return
# example input:
# make_tags('i','Hello world')
# example output:
# <i>Hello world</i>
</syntaxhighlight>


</div>
</div>
[[Category:problem set]]
[[Category:problem set]]
[[Category:python]]
[[Category:python]]
[[Category:strings]]
[[Category:strings]]

Latest revision as of 15:27, 18 September 2019

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. It is also designed to test your computational thinking.

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]