473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

cgi script to run tictactoe

This is my perl program for tictactoe. Can anyone help me run the program as a cgi script?
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. # Description: This program implements the game of Tic Tac Toe
  3. #              The grid looks like the following.
  4. #
  5. #                  +---+---+---+
  6. #                  | 0 | 1 | 2 |
  7. #                  |---+---+---|
  8. #                  | 3 | 4 | 5 |
  9. #                  |---+---+---|
  10. #                  | 6 | 7 | 8 |
  11. #                  +---+---+---+
  12.  
  13.  
  14.  
  15. use strict;
  16.  
  17. # define a constant value
  18. use constant CLEAR => 'cls';
  19.  
  20. # Initial introduction message
  21. my $msg =<<END;
  22. This is the game of Tic Tac Toe. The game is played by two players,
  23. X and O. Wait wait for programs prompt and then make your move by 
  24. entering the coordinates of the location your want to occupy in the
  25. grid.
  26. END
  27.  
  28. system(CLEAR);
  29. print $msg;
  30.  
  31. print "\nPlease press return when ready to continue ... ";
  32. <STDIN>;
  33.  
  34. # use system function to run system commands
  35. system(CLEAR);
  36.  
  37. my $edge_row = '+---+---+---+';
  38. my $mid_row  = '|---+---+---|';
  39.  
  40. # this array holds the content of the grid locations
  41. my @data;
  42.  
  43. init_grid(\@data);
  44.  
  45.  
  46. my @players = ('X', 'O');
  47. my $game_over = 0;
  48. my $player = 'X';
  49. my $err = 0;
  50.  
  51. for(my $turn = 0; !$game_over; $turn = !$turn)
  52. {
  53.     # clear and print grid if no errors
  54.     if(!$err)
  55.     {
  56.     system(CLEAR);
  57.     print_grid(@data);
  58.     }
  59.     # clear the error
  60.     $err = 0;
  61.  
  62.     print "\n\nPlayer $players[$turn] please make your move? ";
  63.     chomp(my $move = <STDIN>);
  64.     my ($x, $y) = split ' ', $move;
  65.  
  66.     if($x < 1 || $x > 3 || $y < 1 || $y > 3)
  67.     {
  68.     # set the error flag
  69.     $err = 1;
  70.     print "\nCoordinates must be between 1 and 3, try again";
  71.     redo;
  72.     }
  73.     my $index = ($x - 1) * 3 + ($y - 1);
  74.     if($data[$index] eq 'X' || $data[$index] eq 'O')
  75.     {
  76.     # set the error flag
  77.     $err = 1;
  78.     print "\nThis location is already occupied, enter another location";
  79.     redo;
  80.     }
  81.     $data[$index] = $players[$turn];
  82.  
  83.     # check if there is a winner
  84.     if(any_winner(@data))
  85.     {
  86.     system(CLEAR);
  87.     print "\nGame over, $players[$turn] wins.";
  88.     $game_over = 1;
  89.     }
  90.     # check if grid is full
  91.     elsif(grid_full(@data))
  92.     {
  93.     system(CLEAR);
  94.     print "\nGame over, nobody wins.";
  95.     $game_over = 1;
  96.     }
  97. }
  98. print_grid(@data);
  99.  
  100. print "\n" x 5;
  101.  
  102.  
  103. #----------------------------------------------------------------------
  104. # Sub        : print_data_row
  105. # input      : data in one row
  106. # output     : none
  107. # Description: pritns one row of the grid in appropriate format
  108. #----------------------------------------------------------------------
  109. sub print_data_row
  110. {
  111.     printf "\n| %s | %s | %s |", $_[0], $_[1], $_[2];
  112. }
  113.  
  114.  
  115. #----------------------------------------------------------------------
  116. # Sub        : print_grid 
  117. # input      : data in grid
  118. # output     : none
  119. # Description: prints the entire grid in appropriate format
  120. #----------------------------------------------------------------------
  121. sub print_grid
  122. {
  123.     @data = @_;
  124.  
  125.     print "\n$edge_row";
  126.     for(my $i = 0; $i < 3; $i++)
  127.     {
  128.     print_data_row($data[0 + 3 * $i],$data[1 + 3 * $i],$data[2 + 3* $i]);
  129.     print "\n$mid_row" if($i < 2);
  130.     }
  131.     print "\n$edge_row";
  132. }
  133.  
  134. #----------------------------------------------------------------------
  135. # Sub        : any_winner
  136. # input      : data in grid
  137. # output     : 1 if there is a winner, 0 otherwise
  138. # Description: checks the grid for a winner
  139. #----------------------------------------------------------------------
  140. sub any_winner
  141. {
  142.     my @data = @_;
  143.  
  144.     for(my $i = 0; $i < 3; $i++)
  145.     {
  146.     # check the columns
  147.     if(($data[$i] eq $data[3 + $i]) && 
  148.        ($data[$i] eq $data[6 + $i]) && $data[$i] ne ' ')
  149.     {
  150.         return 1;
  151.     }
  152.     # check the rows
  153.     if(($data[3 * $i] eq $data[1 + 3 * $i]) && 
  154.        ($data[3 * $i] eq $data[2 + 3 * $i]) && $data[3 * $i] ne ' ')
  155.     {
  156.         return 1;
  157.     }
  158.     }
  159.     # check diagonals
  160.     if(($data[0] eq $data[4]) && 
  161.        ($data[0] eq $data[8]) && $data[0] ne ' ')
  162.     {
  163.     return 1;
  164.     }
  165.     elsif(($data[2] eq $data[4]) && 
  166.       ($data[2] eq $data[6]) && $data[2] ne ' ')
  167.     {
  168.     return 1;
  169.     }
  170.     return 0;
  171. }
  172.  
  173. #----------------------------------------------------------------------
  174. # Sub        : grid_full
  175. # input      : grid data
  176. # output     : 1 if grid full, 0 otherwise
  177. # Description: checks to see if the grid is full
  178. #----------------------------------------------------------------------
  179. sub grid_full
  180. {
  181.     for(my $i = 0; $i < 9; $i++)
  182.     {
  183.     if($data[$i] eq ' ')
  184.     {
  185.         return 0;
  186.     }
  187.     }
  188.     return 1;
  189. }
  190.  
  191. #----------------------------------------------------------------------
  192. # Sub        : init_grid
  193. # input      : reference to the grdi data
  194. # output     : none
  195. # Description: initializes all elements in the grid to ' '
  196. #----------------------------------------------------------------------
  197. sub init_grid
  198. {
  199.     my $ref = shift;
  200.     for(my $i = 0; $i < 9; $i++)
  201.     {
  202.     $ref -> [$i] = ' ';
  203.     }
  204.     return;
  205. }
Oct 23 '06 #1
0 1995

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mike Daniel | last post by:
I am attempting to use document.write(pageVar) that displays a new html page within a pop-up window and the popup is failing. Also note that pageVar is a complete HTML page containing other java...
9
by: wizardRahl | last post by:
Hello, I'm attempting to write a TicTacToe program for class and need some help with arrays. We have to write a program that will allow two users to play tic-tac-toe. The program needs to have...
7
by: imatts | last post by:
Hi can anyone help with this little problem. I have a simple script to swap between two divs on a page. It works perfectly in Firefox & Safari & Opera. It fails in IE 6 giving Object Required error...
7
by: Warren Hoskins | last post by:
Old title: Homework Due 2-20-07 can"t understand why this will not compile. I've been working on tis all week end. Need Help desperately
1
by: racshah | last post by:
I have a tictactoe script with 2 users playing. I need a perl script in which one user plays with the computer. Can anyone help me with it???
0
by: Sean McIlroy | last post by:
""" AUTHOR: Sean McIlroy LANGUAGE: Python 2.5 OVERVIEW: instances of "tictactoeplayer" play optimal tictactoe SPECIALIZED TYPES: player={ex,oh}; empty={blank}; cell=player+empty; board= TYPE...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
10
oedipus
by: oedipus | last post by:
I am currently learning java at school and thus far we have oly dealt with the text-based compiler JGrasp; therefore; my attempt to code Tictactoe or "X and O's" game is written as a text based...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.