Tutorial Moodle VPL Chessboard -- Python
--D. Thiebaut (talk) 17:00, 11 June 2014 (EDT)
This tutorial illustrates how to setup a VPL activity that evaluates a python program that
- prompts the user for an integer N
- display an NxN chessboard of alternating cells on stdout.
This VPL activity uses an extra python program to filter out extra information from the output.
Example User Interaction
Here is an example of the interaction of the user program:
Please enter dimension of board: 8
###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###
###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###
###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###
###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###
VPL Activity Description
Chessboard
Due date: Sunday, 19 June 2016, 8:00 PM Requested files: chessboard.py (Download) Type of work: Individual work Grade settings: Maximum grade: 100 Run: Yes Evaluate: Yes Automatic grade: Yes prints a 2-dimensional chessboard Requested files
chessboard.py
# put your code here
Execution files
vpl_run.sh
#! /bin/bash echo "#! /bin/bash" > vpl_execution echo "python3.4 runChessboard.py" >> vpl_execution chmod +x vpl_execution
vpl_debug.sh
vpl_evaluate.sh
vpl_evaluate.cases
case = Test 1
input = 0
output = "
"
case = Test 2
input = 1
output = "###
###
###
"
case = Test 3
input = 8
output = "###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###
###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###
###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###
###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###
"
<br />
====runChessboard.py====
<br />
:<source lang="python">
#runChessboard.py
import sys
#--- get dimension of chessboard
N = input( "" )
#--- put N in a text file that will be used as stdin for chessboard.py
file = open( "fakestdin.txt", "w" )
file.write( N+"\n" )
file.close()
#--- make stdin read information from the text file
sys.stdin = open( "fakestdin.txt", "r" )
#--- capture the stdout of the program to test into a file
saveStdOut = sys.stdout
sys.stdout = open( "fakestdout.txt", "w" )
#--- run the program to test ---
import chessboard
#--- filter out junk from output of program ---
sys.stdout.close()
sys.stdout = saveStdOut
file = open( "fakestdout.txt", "r" )
text = file.read()
index = text.find( "#" )
text = text[index:]
text = text.strip( ).strip( "\n" ) + "\n"
print( text, end="" )
file.close()
This concludes this tutorial