I am currently working on an open source project with the goal of producing a tetris game that can run in the terminal. I'm currently stuck on the third test. I have written code that passes the first test. and the second test. and passes the eye test for test 3. But test 3 requires taking the output from test 2 and inserting at the start of test 3. I figured I would do this by appending it '>>'. that's where the problem comes in. The first test is to save a file called learntris.py to the testris folder and then run it with:
testris.py --shell ./learntris.py
I pass it every time. Then it runs the code in learntris.py to produce the tetris board output. It runs the 'p' command to print out the board. the tetris board in this game is 10x22 matrix made of '.'. This is the code in learntris.py to pass test 2:
def test2():
while True:
qp = raw_input('')
if qp == 'p':
for i in range(0, 22):
for j in range(0, 10):
print'.',
print''
else:
return exit()
test2()
For more info on the test 2 output you can go here: https://github.com/LearnProgramming/learntris/blob/master/testplan.org#establish-a-way-to-print-the-matrix-p
Now comes test 3, which is the g command that prints the tetris shapes. They are represented by various letters. This is the text output you receive after failing test 3:
The 'g' command instructs learntris to read 22 lines
of text from the standard input stream, and use the
characters on these lines to populate some internal
representation of the matrix.
The letter 'g' is a mnemonic for the word 'given', as
in: "given the following matrix...."
The input format should be identical to the output
produced by the 'p' command.
The letters used in the representation correspond to
the set of colors used for blocks in the game:
. = empty (black) b = blue c = cyan
g = green m = magenta o = orange
r = red y = yellow
This is the code I am using for test 3. I know there is supposed to be some sort of standard input/output to receive the output from the 'p' command but I'm not sure what to do there. The code is mostly focused on reproducing the correct output which you can see here: https://github.com/LearnProgramming/learntris/blob/master/testplan.org#establish-a-way-to-set-the-entire-matrix-g
def test3():
while True:
qp = raw_input('')
if qp == 'g':
for i in range(0, 4):
for j in range(0, 10):
print'.',
print''
for i in range(4, 5):
for j in range(0, 10):
print'm',
print''
for i in range(5, 6):
for j in range(0, 10):
print'b',
print''
for i in range(6, 7):
for j in range(0, 10):
print'c',
print''
for i in range(7, 8):
for j in range(0, 10):
print'y',
print''
for i in range(8, 9):
for j in range(0, 10):
print'o',
print''
for i in range(9, 10):
for j in range(0, 10):
print'r',
print''
for i in range(11, 15):
for j in range(0, 10):
print'.',
print''
for i in range(15, 17):
for j in range(0, 1):
print'c',
for j in range(1, 10):
print'.',
print''
for i in range(17, 18):
for j in range(0, 1):
print'c',
for j in range(1, 5):
print'.',
for j in range(5, 6):
print'g',
for j in range(6, 10):
print'.',
print''
for i in range(18, 19):
for j in range(0, 1):
print'c',
for j in range(1, 3):
print'.',
for j in range(3, 4):
print'o',
for j in range(4, 5):
print'.',
for j in range(5, 7):
print'g',
for j in range(7, 10):
print'.',
print''
for i in range(19, 20):
for j in range(0, 3):
print'.',
for j in range(3, 4):
print'o',
for j in range(4, 5):
print'.',
for j in range(5, 6):
print'b',
for j in range(6, 7):
print'g',
for j in range(7, 10):
print'.',
print''
for i in range(20, 21):
for j in range(0, 1):
print'.',
for j in range(1, 2):
print'm',
for j in range(2, 4):
print'r',
for j in range(4, 6):
print'o',
for j in range(6, 7):
print'b',
for j in range(7, 9):
print'y',
for j in range(9, 10):
print'.',
print''
for i in range(21, 22):
for j in range(0, 3):
print'm',
for j in range(3, 5):
print'r',
for j in range(5, 7):
print'b',
for j in range(7, 9):
print'y',
for j in range(9, 10):
print'.'
print''
else:
return exit()
test3()
Again, I was just focused on getting the correct output. So I now from the sentence "input format should be identical to the output format produced by the 'p' command" that I need to append learntris.py to include the code for test3 - located on whatever.py. Or do I have to append whatever.py with the code from learntris.py? I'm having trouble appending in the terminal. I tried going learntris.py > whatever.py to overwrite all that code with test 2 code but it just deleted all the test3 code and didn't replace it with test2 code. It seems the only way for me to append anything is to run it with testris.py. When I go:
./testris.py --shell ./learntris.py >> ./whatever.py
it results in the terminal just printing another line. Just another MacBook-Pro: learntris user$ line getting printed. But when I check the whatever.py file I see that all the output you get from failing test 3 is just added to the end of the test 3 code. When I go
./testris.py --shell ./whatever.py >> ./learntris.py
it just adds the failed test 2 output to the end of the learntris.py code. So I'm guessing that '>>' is for appending text output only? Or am I missing something in the front of my test 3 code to catch the p command output? also is there a more efficient way to write my code for test 3?