0

I am kind of new in programming (I only know python). Yesterday I finished making a 2048 game (running in the command line). Now I decided to make an AI that would auto-play, but I don't want this AI to run each time I run the game.

So to my question, is there a way to make a program/script to actually input (in the command line) and read data from another program?

My "move" code from my 2048 game:

def move():
    global board
    direction = "x"
    while direction != "a" and direction != "w" and direction != "s" and direction != "d":
          direction = raw_input("Where do you want to move?(WASD)")
          direction = direction.lower()
          if direction == "a":
             left()
          elif direction == "w":
             up()
          elif direction == "s":
             down()
          elif direction == "d":
             right()

So what I want is a second program that is actually inputting in the( direction = raw_input("Where do you want to move?(WASD)")) instead of the player.

Is there a way to do this? Edit: I am running windows 10

Feconiz
  • 39
  • 9
  • Which operating system are you running? – Snild Dolkow Aug 25 '15 at 17:30
  • 1
    You could pipe the output from the other program into it. – kyle k Aug 25 '15 at 17:31
  • @kylek I am actually a newbie(programming for about a month and a half) so if you could be a bit more specific – Feconiz Aug 25 '15 at 17:35
  • You could also just have the AI write the move to a text file and then read that file from your 2048 code to retrieve it. – pzp Aug 25 '15 at 17:50
  • @Feconiz if you are using a unix system then you can redirect the output from one program to another using the pipe character like this `./script1.py | ./script2.py` that will feed each line of the output from script 1 into script 2, so each call of raw_input will read a line from the output of the other script. – kyle k Aug 25 '15 at 17:54
  • could you please be a bit more specific ?how would I do that ? @kylek – Feconiz Aug 26 '15 at 15:30

2 Answers2

0

You have to replace the call for raw_input to call you custom function that handles the input.

Something like:

from my_other_script import get_input

def move():
    global board
    direction = "x"
    while direction != "a" and direction != "w" and direction != "s" and direction != "d":
          direction = get_input(board)
          if direction == "a":
             left()
          elif direction == "w":
             up()
          elif direction == "s":
             down()
          elif direction == "d":
             right()

And in my_other_script.py have a function named get_input that does whatever you want to generate the input

def get_input(board):
    # my conditions go here
    return direction

Note that I'm passing the board so you can know what is current board state and use it to choose the best move.

If you CAN'T replace the raw_input call, you can monkey patch it using the mock module.

f.rodrigues
  • 3,499
  • 6
  • 26
  • 62
  • Thank you :) I hadn't thought of that – Feconiz Aug 25 '15 at 17:40
  • hmmm that doesn't seem to work...have i done something wrong ? (both files are in the same folder) from 2048_Ai.py import get_input (it throws invalid syntax) – Feconiz Aug 25 '15 at 17:51
  • drop the `.py`, just use `from 2048_Ai import get_input` – f.rodrigues Aug 25 '15 at 17:54
  • actually the 2048 messed it up even after removing the .py.....so now its just named Ai :) thanks – Feconiz Aug 25 '15 at 18:01
  • Oh yeah, python doesn't allow variables to start with numbers (and import is the same as setting them with a variable name), check this answer on how to do that. http://stackoverflow.com/questions/9090079/in-python-how-to-import-filename-starts-with-a-number – f.rodrigues Aug 25 '15 at 18:11
0

No this would be very tough and compilicated.

I would recommend incorporating the AI into you're script, and then giving the user the option of playing as either him/herself or allowing the AI to play:

print "Would you like to play, or watch the AI play?"
print " 1) User"
print " 2) AI "
userOrAI = raw_input("")
if (userOrAI == "1"):
    move()
if (userOrAI == "2"):
    AImove() #you would need to create a function to play as AI here
else:
   print "Invalid entry"
jstm
  • 420
  • 4
  • 14
  • yes i could do that also ,now that you mentioned it i can see how silly is my question.....thanks – Feconiz Aug 25 '15 at 17:48