473,224 Members | 1,404 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,224 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 1986

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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.