Skip to content

Full set of files to run a game of backgammon in the terminal. Created using python on VSCode without the use of AI.

Notifications You must be signed in to change notification settings

CHKSmith25/backgammon-ascii

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Backgammon

This programme is a terminal-based version of the board game, backgammon. The game is two player, turn based and is played through inputs into the terminal. The board and dice rolls are printed to the terminal using ascii art throughout the game. See the below preview copied and pasted from the terminal:

---Player 0 | Black Counter---
Type any character to roll your dice: s
┌─────────┐  
│  ●   ●  │  
│         │  
│  ●   ●  │  
└─────────┘  
---Player 1 | White Counter---
Type any character to roll your dice: d
┌─────────┐  
│  ●      │  
│         │  
│      ●  │  
└─────────┘  
Black Counters start with dice roll 4 and 2
——————————————————————————————————————————————————————
      l k j i h g     f e d c b a   black bear-off: 0
     ============================= 
    |. . . o . | | o . . . .|   
    |. . . o . | | o . . . .|   
    |. . . o . | | o . . . . . |   
    |. . . . . | | o . . . . . |   
    |. . . . . |0| o . . . . . |   
    |             | |             | 
    | o . . . . . |0|. . . . . |   
    | o . . . . . | |. . . . . |   
    | o . . .. | |. . . . . |   
    | o . . .. | |. . . . o |   
    | o . . .. | |. . . . o |   
     ============================= 
      m n o p q r     s t u v w x   white bear-off: 0
——————————————————————————————————————————————————————
Select a counter position (a to x) or enter 'skip' if you can't move: 

The game operates under the regular rules of backgammon as can be found via the link below (please note that this game does not account for the doubling feature as described in number 5 of the rules):

https://www.farrar-tanner.co.uk/blog/post/set-up-rules-of-play/

Installation

This application doesn't require the installation of any library's. It was made using python 3.14.2, however is executable on earlier versions.

Usage

There are two files required to run this game: main.py and helpers.py. tests.py contains some tests used in development and is included for transparency.

Start The Game

To begin the game, run the following code in the terminal (python command may vary depending on which OS is used):

python3 main.py

The first player will then be prompted to roll their first dice, followed by the second player, as can be seen from the ascii art copied at the top of this README.

Re-roll

If the players roll the same dice in the game start, they are prompted to re-roll. This is because there needs to be a player with a highest roll to determine who makes the first turn.

---Player 0 | Black Counter---
Type any character to roll your dice: f
┌─────────┐  
│  ●      │  
│         │  
│      ●  │  
└─────────┘  
---Player 1 | White Counter---
Type any character to roll your dice: f
┌─────────┐  
│  ●      │  
│         │  
│      ●  │  
└─────────┘  
Draw: Players rolled same number! Let's roll again...
---Player 0 | Black Counter---
Type any character to roll your dice: 

Main Game

A player begins their turn by making a dice roll. If the roll is a double, they get 4 moves, rather than 2, as per backgammon rules.

---Player 1's Turn---
Type any character to roll your dice: s
┌─────────┐  ┌─────────┐  
│  ●      │  │  ●      │  
│         │  │         │  
│      ●  │  │      ●  │  
└─────────┘  └─────────┘  

To make a move, the player whose turn it is must select a start position where they want to move their counter from. Each position is indicated by a letter from a to x. The input must be a single letter and must be where the player has a counter, else an error message is printed and they are re-prompted.

The player is then prompted to select a position to move that counter to or go back to select a different start. The white counters move from a to x, and black counters move from x to a. Below is a snippet of the process of making a move, where invalid inputs result in error messages and re-prompting.

Select a counter position (a to x) or enter 'skip' if you can't move: r
Please select a position where you have a counter!
Select a counter position (a to x) or enter 'skip' if you can't move: q
Select a position to move that counter to (a to x) | enter 'back' to choose a different position | or enter 'skip' if you can't move: back
Select a counter position (a to x) or enter 'skip' if you can't move: skdjs
Error: please input the unique letter representing the position
Select a counter position (a to x) or enter 'skip' if you can't move: 5
Error: Please select a letter between a and x
Select a counter position (a to x) or enter 'skip' if you can't move: q
Select a position to move that counter to (a to x) | enter 'back' to choose a different position | or enter 'skip' if you can't move: n
Please select a position corresponding to your rolls [5, 4]
Select a position to move that counter to (a to x) | enter 'back' to choose a different position | or enter 'skip' if you can't move: u
rolls left: [5]

Once a move is made, the updated board is re-printed, so the move can be seen.

Skip

There is an additional feature to skip a turn. There can be rare cases where a player cannot make a move with the dice they have rolled. This is where they would need to skip there turn and pass over to the other player.

Select a counter position (a to x) or enter 'skip' if you can't move: skip
---Player 0's Turn---

End of turn

Once all dice rolls are used, or the player has skipped, the turn passes over to the next player who then rolls and takes their turn. These turns continue until a winner is declared.

Eliminated Counters

If a player moves their counter to a position where the opponent has exactly one counter, they eliminate that counter and it is placed on the 'bar'. The tally for the eliminated counters is kept in the middle of the board (see the board below), with the count for the eliminated black counters in the top half and that for the white counters in the bottom half. The player, whose counter got eliminated, must use a roll to return that counter back to their starting corner of the board before they can make any other move.

---Player 0's Turn---
Type any character to roll your dice: d
┌─────────┐  ┌─────────┐  
│  ●   ●  │  │  ●      │  
│  ●   ●  │  │         │  
│  ●   ●  │  │      ●  │  
└─────────┘  └─────────┘  
——————————————————————————————————————————————————————
      l k j i h g     f e d c b a   black bear-off: 0
     ============================= 
    | ● . . . o o | | o . . . . ● |   
    | ● . . . o . | | o . . . . ● |   
    | ● . . . o . | | o . . . . . |   
    | ● . . . . . | | o . . . . . |   
    | . . . . . . |1| o . . . . . |   
    |             | |             | 
    | . . . . . . |0| ● . . . . . |   
    | o . . . . . | | ● . . . . . |   
    | o . . . . . | | ● . . . . . |   
    | o . . . ● . | | ● . . . . . |   
    | o . . . ● . | | ● ● ● . . o |   
     ============================= 
      m n o p q r     s t u v w x   white bear-off: 0
——————————————————————————————————————————————————————
Select a counter position (x to s) to move eliminated counter to: w
rolls left: [6]

Bearing off

Once a player has all their counters into their home corner of the backgammon board, they can enter into the bearing off phase. This is where they select counters to remove from the board, based on their rolls (see the bearing off rules at the top of this README). All the bearing off rules are enforced through error messages and re-prompts.

Select a white counter position (s to x): w
Please select a position where you have a counter!
Select a white counter position (s to x): s
Select a position to move to (s to x) | or type 'back' to select a different counter:  x
Error: You must use this roll to bear off
Select a white counter position (s to x): t
rolls left: [3]

Declaring winner

The first player to bear off all of their counters (15) is declared winner.

Select a white counter position (s to x): x
——————————————————————————————————————————————————————
      l k j i h g     f e d c b a   black bear-off: 13
     ============================= 
    | . . . . . . | | . . . . . o |   
    | . . . . . . | | . . . . . o |   
    | . . . . . . | | . . . . . . |   
    | . . . . . . | | . . . . . . |   
    | . . . . . . |0| . . . . . . |   
    |             | |             | 
    | . . . . . . |0| . . . . . . |   
    | . . . . . . | | . . . . . . |   
    | . . . . . . | | . . . . . . |   
    | . . . . . . | | . . . . . . |   
    | . . . . . . | | . . . . . . |   
     ============================= 
      m n o p q r     s t u v w x   white bear-off: 15
——————————————————————————————————————————————————————
Player 1 Wins!

Roadmap

Areas for improvement

General Comments

As will be clear from helpers.py, in particular, there are several areas for improvement. I have bullet pointed some key areas for improvement below:

  • Abstraction: functions could be abstracted much further so that they only perform one thing. Particularly, this would help with the following areas:
    • Inputs abstracted out of the get_valid_input() function would make the function clearer and more repeatable, rather than having an integer input as a parameter to select required input message.
    • Main game functions were copied and slightly tweaked to make all the bearing off functions. This could have been avoided with simpler and more separated functions.
  • Clearer variable and function names. Single letters were used for some variables as parameters to functions, which aren't descriptive enough.
  • Modelling and creating flow diagram at the beginning: programme structure wasn't fully formalised before approaching writing the code. With this project requiring many functions, it would have been helpful to define a clear structure before approaching the code.
    • This may have helped spot the challenges in the bearing off functions
    • This also may have saved time and reduced the effort made in going back to change functions
  • Testing: taking a TDD approach would have enforced a simpler structure for functions and made the building blocks for the programme a lot sturdier. Code would have been cleaner and more repeatable, especially when comparing the main game and the bearing off functions.

End game bug

There is a bug at the end of the game. A winner is not declared if the player completes bearing off in the middle of their turn, when they still have rolls left. This would be a simple fix by including the check_winner() function inside the loop for making a move in take_turn

License

MIT

About

Full set of files to run a game of backgammon in the terminal. Created using python on VSCode without the use of AI.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages