connect 4 #1520
euclidaeun
started this conversation in
Libraries and Code Samples
connect 4
#1520
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Connect 4 is a game where two players takes turns to place a coin,
the first player to connect a row of 4 coins wins, the row could be either horizontal, vertical or diagonal
The game and features are done by a bot
Features
Creating and Manipulating Lists
The code demonstrates how to use lists to store and manage game data such as the game board, rows, and columns.
NewList() and NewStringList() are used to create new lists.
ListItem() is used to retrieve items from lists, and list add() is used to add elements to lists.
Iterating Over Lists
The code shows how to use for loops to iterate over lists (e.g., iterating through rows and columns of the board).
This is helpful for manipulating or printing out grid-based structures like a game board.
Dynamic User Input Handling
The get input script command is used to capture player input (in this case, the column number for dropping a coin).
ToInt() is used to convert the player's input from text to an integer.
Input Validation
The code validates the player's input by checking if the entered column is between 0 and 6. If it's invalid, a message is shown to prompt the player again.
Conditionally Updating the Game State
Conditional logic is used to check if a cell in the column is empty (represented by "0") and only allows the player to place a coin if the position is available.
coin_placed is used as a flag to stop further iterations once a coin is placed.
String Manipulation
The board uses strings (e.g., "0", "b", "w") to represent empty spots and coin placements.
String manipulation techniques are used when modifying rows to update the game state (placing a "b" for a black coin or a "w" for a white coin).
Replacing List Items
List replace logic is simulated using list remove() and list add() to replace old rows with updated rows, ensuring that the board reflects the current game state.
Displaying Dynamic Content
The msg() function is used to display messages to the player, including updating them on which column they chose and printing the game board.
Custom Functions
Custom functions like print_board are defined to handle repetitive tasks, such as displaying the game board. This helps reduce code duplication and improve readability.
Multiple Objects and Interactions
The code demonstrates how to create objects (e.g., black_coin, white_coin) and associate actions with them (e.g., using a coin to place it on the board).
The objects have verbs associated with them, such as "Use", which trigger the corresponding action when the player interacts with them.
How to paste the sample code
Download quest for windows at https://textadventures.co.uk/quest
This is the offline editor (Online editor is not able to copy and paste the whole game code)
Startup your quest gamebook/textadventure, on the right side of the big play button, you can see a code view button </>
Copy my code to replace the code in the text box, click code view button again.
Viola, it is done, press play button to test out the game and modify the code to your preference.
Connect 4
Beta Was this translation helpful? Give feedback.
All reactions