Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7003289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:02:57+00:00 2026-05-27T21:02:57+00:00

I’m writing a simple command-line board game in Ruby. I need to have main

  • 0

I’m writing a simple command-line board game in Ruby. I need to have main game code and then independent code for the two players that are going to be written by two different people or teams. So the main game needs to allow different players to play without changing any code.

I’ve been thinking of ways to do this, but this is the biggest programming project I’ve ever undertaken and I frankly don’t have a good Idea yet.

Should both player-programms define a method that the main game calls? Should I have a Player class and have the player-Programms subclass it? I tried a few things but it never seems to be what I was looking for or what would be considered even decent design.

What would be a good way to do this in Ruby (or any other language, as the problem is a general programming problem)?

I hope I was able to phrase the question correctly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:02:57+00:00Added an answer on May 27, 2026 at 9:02 pm

    I’d say that the subclass approach sounds good (in other languages, interfaces might be a better choice, but I don’t think interfaces exist in Ruby). The method that each player class must implement should take as a parameter an object that contains the entire game state (such as the piece positions if you are making chess). This object must be read-only or must be a copy of the game state, so that the player classes may not directly alter the game state. Instead, the method that the player classes must implement should return some value that indicates what move the player wishes to do. I suppose that Ruby supports some mechanism to dynamically instantiate a class from another file based on the names of the class and the file. You could start the game like this, using parameters to indicate the file and class names:

    ruby game.rb player1.rb Player1Class player2.rb Player2Class
    

    The game would probably look like this pseudocode:

    gameState = createObjectDescribingInitialGameState()
    player1FileName = args[0]
    player1ClassName = args[1]
    player2FileName = args[2]
    player2ClassName = args[3]
    player1 = dynamicallyInstantiateClassFromFile(player1ClassName, player1FileName)
    player2 = dynamicallyInstantiateClassFromFile(player2ClassName, player2FileName)
    currentPlayer = player1
    while not gameState.isGameOver():
        move = currentPlayer.makeMove(gameState.createReadOnlyCopy())
        gameState.peformMove(move)
        currentPlayer = player2 if currentPlayer == player1 else player1
    

    With this approach, you can write the core game code once and launch it without modification with any pair of player classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.