Tutorial Moodle VPL Chessboard -- Python

From dftwiki3
Revision as of 19:45, 12 June 2014 by Thiebaut (talk | contribs)
Jump to: navigation, search

--D. Thiebaut (talk) 17:00, 11 June 2014 (EDT)



MoodleVPLLogo.png



Moodle VPL Tutorials



This tutorial builds on the first one in the series, which should be done first, as the present tutorial skips some steps. Here we illustrate how to setup a VPL activity that evaluates a python program that

  1. prompts the user for an integer N
  2. display an NxN chessboard of alternating cells on stdout.

This VPL activity uses an additional python program to filter out extra information from the output of the student program.


Example User Interaction


Here is are examples of interactions with the solution program:

Please enter dimension of board: 0

(no output from the 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_evaluate.cases


case = Test 1
input = 0
output = "
"
 
case = Test 2
input = 1
output = "###
###
###
"
case = Test 3
input = 8 
output = "###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###
###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###
###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###
###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###
"

runChessboard.py

#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()


Testing

  • Click on VPL administration, then Test activity. Enter the program below in the chessboard.py program area, and Evaluate.


def printBlock( visible ):
    
    if visible==True:
        print( "###", end="" )
    else:
        print( "...", end="" )


# Makes indicated number of 3x3 cell(s) and alternates
# black (###) and white (...) cells for each row
def printLine( visibility, isOdd, dimension ):

    for i in range( 3 ):
        for i in range( dimension//2 ):
            printBlock( visibility )
            printBlock( not visibility )
        
        if isOdd:
            printBlock( visibility )

        print()


# Alternates black (###) and white (...) cells the number of
# times that the user indicated for each column
def repeatLine( dimension, isOdd ):

    for i in range( dimension//2 ):
        printLine( True, isOdd, dimension )
        printLine( False, isOdd, dimension )

    if isOdd:
        printLine( True, isOdd, dimension )


def main():
    # Asks user for dimension of chessboard
    dimension = int( input( "Enter dimension of board: " ) )

    # Asks user again for another input
    # if user enters negative number
    while dimension < 0:
        dimension = int( input( "Invalid input. Please enter 0 or positive number: " ) )

    # Boolean variable when dimension is
    # odd for the for loops in functions
    isOdd = dimension % 2 == 1

    # prints chessboard
    repeatLine( dimension, isOdd )


main()


  • Verify that the submitted program gets a 100 mark for all 3 tests.


MoodleVPLChessboardTest.png




This concludes this tutorial