CSCI 150, Spring 2003

Home | | Course Schedule | | Assignments | | Lecture Notes

Homework 2 solution

Decision tree solution
Click here for an image of the Decision Tree

Program solution:

{Program: nim.pas
Author: Brenda Student
Class: CSCI 150, Section 03
Date: 2/7/03
Assignment: Homework 2
Purpose: Plays the game of Nim with the user.}

program Nim;
var
	move1: string;  {user's first move.}
	move2: string;  {user's second move.}
begin
	writeln('What is your move?');
	readln(move1);
	if move1 = 'X' then
	begin
		writeln('The board is X.');
		writeln('I play O.');
		writeln('The board is XO.');
		writeln('What is your next move?');
		readln(move2);
		if move2 = 'X' then
		begin
			writeln('The board is XOX.');
			writeln('I play OOO');
			writeln('The board is XOXOOO.');
			writeln('I win!');
		end
		else if move2 = 'XX' then
		begin
			writeln('The board is XOXX.');
			writeln('I play OO.');
			writeln('The board is XOXXOO.');
			writeln('I win!');
		end
		else
		begin
			writeln('The board is XOXXX.');
			writeln('I play O.');
			writeln('The board is XOXXXO.');
			writeln('I win!');
		end;
	end
	else if move1 = 'XX' then
	begin
		writeln('The board is XX.');
		writeln('I play O.');
		writeln('The board is XXO.');
		writeln('What is your next move?');
		readln(move2);
		if move2 = 'X' then
		begin
			writeln('The board is XXOX.');
			writeln('I play OO.');
			writeln('The board is XXOXOO.');
			writeln('I win!');
		end
		else if move2 = 'XX' then
		begin
			writeln('The board is XXOXX.');
			writeln('I play O.');
			writeln('The board is XXOXXO.');
			writeln('I win!');
		end
		else
		begin
			writeln('The board is XXOXXX.');
			writeln('You win!');
		end;
	end
	else
	begin
		writeln('The board is XXX.');
		writeln('I play OOO.');
		writeln('The board is XXXOOO.');
		writeln('I win!');
	end;
end.