CSC1002 – Computational LaboratoryCSC1002 – 2023 Winter By Kinley LamConnect 4 - 2023

该任务要求设计并开发一个基于TurtleGraphics的2玩家连接四游戏。玩家轮流在8x8的游戏板上放置颜色标记,首个连成一行四个相同颜色标记的玩家获胜。游戏结束时,用不同颜色突出显示获胜的标记行。程序需处理鼠标点击、追踪鼠标移动并在适当位置高亮列。代码应遵循逻辑分解原则,仅使用函数,不使用自定义类,并将所有代码放在单个文件中。
摘要由CSDN通过智能技术生成

Connect 4 - 2023

Overview

In this assignment, you are going to design and to develop a 2-player connection board game, in which the players first choose a color called token and then take turns dropping the colored token into columns, and the first player who connects four tokens of same color before the other opponent does wins the game. The four connected tokens must be placed in four consecutive positions in a row, be it horizontally, vertically or diagonally (left and right). The following 3 diagrams show 3 different winning connections (yellow tokens) in horizontal, vertical and diagonal positions respectively.

 

Scope

  1. Implement the board game using the standard Turtle Graphics module. The game board is a fixed 8-column by 8-row grid. The following diagram shows the overall layout of the game:

Status Bar

Mouse Pointer

Connected Tokens

Colored Tokens

 

  1. Column Trackers

    Token Placeholder

    Status Bar
    1. It’s the standard title bar of the Turtle Graphics’ Windows. Your program will use it to display a simple string to show the FINAL status of the game:
      1. Winner ! Player 1, or
      2. Winner ! Player 2, or
      3. Game Tied !

Note: during the game you can simply display a default, static title such as “Connect 4”, optionally including the player identifier, such as Connect 4 – Player 1 Turn, or Connect 4 – Player 2 Turn.

Hint: use title() from the Screen object to set the text string in the title bar.

  1. Connected Tokens
    1. At end of the game, if there is a winner, outline the 4 connected tokens with another different color (red as shown in the diagram above). Note that it’s possible there are multiple winning rows such as the horizontal and diagonal rows in blue as shown in the following diagram. In this case, your program simply shows any one of the winning rows.

  1. Colored Tokens

 

    1. Use standard shape such as circle or square to implement colored tokens and pick a different color for each player (ex: blue and purple as shown). Set both width and height of the token shape to 60 pixels. The default size in pixels for standard circle and square shapes is 20. Hint: for turtle object use function shapesize() to stretch the size accordingly.
  1. Column Trackers

 

    1. Show the column positions of the game board. Separate column trackers with small margin in between, including to the left side of the first column and to the right of last column (see diagram above). Match the width of each column tracker to the colored token’s shape, and set the height of each column tracker to 20 pixels (default size).
  1. Token Placeholder

 

    1. Based on the current position of the mouse pointer, outline the corresponding column tracker with the color that matches that of the current player. Set the width of the outline to around 5 pixels. Hint: for turtle object you could use color() to set the outline color and use shapesize() to set the width of the outline. See “Mouse Tracking” for more info.
  1. Size & Colors
    1. You are free to choose your own set of colors (fill and outline combined) provided that different colors are consistently used for different components such as color tokens, column tracker, token placeholder, connected tokens and so on.
    2. Stick to the measurement (pixel size) suggested for tokens and column trackers, or choose a different measurement appropriately.
  2. Token Dropping

    1. Mouse click is used to initiate the action of dropping a token to the currently outlined column (token placeholder). It’s not mandatory to implement the logic to show the dropping of the token from the top of the column. In other words, your program can instantly and directly display the token in the column at the correct position right after the mouse click.
  1. Mouse Tracking
    1. You are only required to track the mouse pointer when it’s hovered within the Turtle Graphics’ window. Therefore, as the mouse pointer hovers out of the Turtle Graphics’ window, leave the outlined column as it is. As soon as the mouse pointer is within the Turtle Graphics’ window, your tracking logic should outline the column correspondingly.
    2. Hint: use bind() function from the canvas object to bind the mouse motion to a separate function. To get the canvas object use getcanvas() from the turtle module.
    3. Hint: Note that you need to implement timer logic with a separate function to perform the tracking logic as mentioned above. See “Timer - Screen Refresh” for more info.
  2. Timer – Screen Refresh
    1. In general, if your logic relies on a timer to perform repeated computation at a regular interval ensure that the value of timer rate is set appropriately. For an example, if timer is used for “Mouse Tracking”, you would set a small value for the timer rate, such as 100 msec. If it’s set to a higher timer rate, say 2 seconds (2000 msec), the outlined column position would not accurately match the mouse pointer’s position when it’s in motion.

NOTE:

  • Keep your entire source code in ONE SINGLE file.
  • Use only standard python modules
  • In your design stick ONLY to functions, in other words, no class objects of your own.

Startup Options

Not applicable

Skills

In this assignment, you will be trained on the use of the followings:

  • Logic Decomposition: Functions (with parameters and return) for program structure and logic decomposition
  • GUI based programming – turtle graphics
    • Event, Timer, Mouse Tracking, and so on.
  • Standard objects (strings, numbers & lists)
  • Variable Scope

Deliverables

  1. Program source code (A2_School_StudentID_Source.py)

where School is SSE, SDS, SME, HSS, FE or LHS and StudentID is your 9-digit student ID.

Submit the python file by due date to the corresponding assignment folder under “Assignment (submission)”

For instances, a SME student with student ID “119010001” will name the program as follows:

    • A2_SME_119010001_Source.py:

5% will be deducted if file is incorrectly named!!!

Tips & Hints

    • Beware of variable scope as you might keep a few variables as global such as puzzle
    • Refer to python website for program styles and naming convention (PEP 8)
    • Validate all inputs (if any) - if incorrect input is detected then display a friendly response and prompt the input again.
    • Use Canvas to bind the motion of the mouse pointer to your program
      • The x,y coordinate is upper-left corner based (0,0) and window coordinate.
    • Use separate timer to update the column tracker
    • Use regular turtle objects as tokens and column trackers.
    • Use color() function to change the fill and outline colors of the turtle objects.
    • Use ontimer() function to perform repeated computation at a regular time interval in msec.
    • Use shapesize() function to change the size of the turtle objects.
    • Use setworldcoordinates() function to change the x-y coordinates of the lower left and upper right corners. All drawing follows world coordinate setting. Hint: best to keep the same resolution as the Turtle Graphics’ Window (screen size).

Sample Output

 

Marking Criteria

    • Coding Styles – overall program structure including layout, comments, white spaces, naming convention, variables, indentation, functions with appropriate parameters and return.
    • Design Documentation if required
    • Program Correctness – whether or the program works 100% as per Scope.
    • User Interaction – how informative and accurate information is exchanged between your program and the player.
    • Readability counts – programs that are well structured and easy-to-follow using functions to breakdown complex problems into smaller cleaner generalized functions are preferred over a function embracing a complex logic with nested conditions and sub-functions! In other words, a design with clean architecture with high readability is the predilection for the course objectives over efficiency. The logic in each function should be kept simple and short, and it should be designed to perform a single task and be generalized with parameters as needed.
    • KISS approach – Keep It Simple and Straightforward.
    • Balance approach – you are not required to come up a very optimized solution. However, take a balance between readability and efficiency with good use of program constructs.

ITEMS

Percentage

Remarks

Coding Styles

15%-20%

0% IF PROGRAM DOESN’T RUN

User Interface

15%-20%

0% IF PROGRAM DOESN’T RUN

Functionality

MAX. 70%

Refer to Scope

Due Date

Apr 2nd, 2023, 11:59:59PM

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值