Advertisement
botman7

Tic Tac Toe Java Edition

Apr 14th, 2019
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 28.12 KB | None | 0 0
  1. /**
  2.  * @author Carson Lees
  3.  * Date: 4/14/2019
  4.  * Program: TicTacToe
  5.  */
  6.  
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.util.Random;
  10. import java.util.Scanner;
  11.  
  12. import javax.swing.JFrame;
  13. import javax.swing.JTextArea;
  14. import javax.swing.JTextField;
  15.  
  16. public class TicTacToe extends JFrame{
  17.     public static char N = '_';
  18.     public static char S = '_';
  19.     public static char E = '_';
  20.     public static char W = '_';
  21.     public static char M = '_';
  22.     public static char NW = '_';
  23.     public static char NE = '_';
  24.     public static char SW = '_';
  25.     public static char SE = '_';
  26.     public static boolean Ntaken = false;
  27.     public static boolean Staken = false;
  28.     public static boolean Etaken = false;
  29.     public static boolean Wtaken = false;
  30.     public static boolean NWtaken = false;
  31.     public static boolean NEtaken = false;
  32.     public static boolean Mtaken = false;
  33.     public static boolean SWtaken = false;
  34.     public static boolean SEtaken = false;
  35.     public static boolean win = false;
  36.     public static boolean spaceTaken = false;
  37.     public static boolean scriptStopped = false;
  38.     public static String uText = "";
  39.     public static String output = "";
  40.     public static String screen = "";
  41.     public static String board = "";
  42.     public static String side = "?";
  43.     public static String customTxtEnterText = "";
  44.     public static String winMethod = "";
  45.     public static String status = "x";
  46.     public static char letter = ' ';
  47.     public static void Encoder(String text) {
  48.  
  49.         int disSyn = 1;
  50.         int timesLooped = 0;
  51.         boolean containsDiscordSyntax = false;
  52.         Scanner scanner = new Scanner(System.in);
  53.         String referance1 = "qwertyuiopasdfghjklzxcvbnm1234567890!@#$%^&*()`~-=_+[]{}|;':,.<>/?QWERTYUIOPASDFGHJKLZXCVBNM";
  54.         String referance2 = "-MS=rPNtyuBi45R_+OA[qVw.<EmQYC1J2?d>7F8fg&H*(0!@#9DU~]{hX}|ZbjkWl'opT)`Gzxc;s:,63/aeIn$L%K^v";
  55.         String referanceA1 = ":,63/aeInrPtyuBi4s$L%g&H?d>7F8S=_-EmQYf'opT)`Gzxc;2l!@#9DU~]{hXN+OA[qVw.<ZbjkW*(0C1J5R}|MK^v";
  56.         String referanceA2 = "9D)`[qVvyuBeInSYf'EaQ2lc;ZbjkW*(0C1XN+OAo&w.<!@rU~:=_-7F8#]{i4s$L%ghJ5R}|Mx,6^Hm3/PtGzKpT?d>";
  57.         String referanceA3 = "$L%ghJ9@yuVv*(0C1x,6^rU~:=_XN+Hm3T?dDPteIzKp]{i4s>&w.<!OAoRnS)`[G}|M-7F8#/Yf'EaQ2lc;ZbjkBqW5";
  58.         String referanceA4 = "uV?Q2lcL%~:w.v*(0C1x,6^rUS`[G}W5!OAoRngh{i4s;dDPteq@y-7F8#/)YIzKp]$>&|MJ9=_XN+Hm3T<'EaZbjkBf";
  59.         //setting references
  60.  
  61.         System.out.print("Type something to be translated: ");
  62.         String textHolder = text;
  63.  
  64.         while (disSyn >= 1) {//Loops if discord syntax is met
  65.  
  66.             text = textHolder; //Resets text to original state
  67.  
  68.             disSyn = disSyn - 1;//Subtracts 1 from dysSyn to break overall while loop
  69.  
  70.  
  71.             if (text.length() == 1) {
  72.                 text = "___" + text;
  73.             }
  74.             if (text.length() == 2) {
  75.                 text = "__" + text;
  76.             }
  77.             if (text.length() == 3) {
  78.                 text = "_" + text;
  79.             }
  80.             //Encrypter only works with 4 characters or more, to avoid errors, underscores are added.
  81.  
  82.  
  83.             int clock = 0;
  84.             int clock2 = 0;
  85.             int txtlengthMinusOne = text.length() - 1;
  86.             int reflengthMinusone = referance1.length() - 1;
  87.             int txtlength = text.length();
  88.             int charAst = 0;
  89.             int charUnd = 0;
  90.             int charTil = 0;
  91.             int charTil2 = 0;
  92.  
  93.             @SuppressWarnings("unused")
  94.             char nextorigchar = 'x';
  95.             char firstCharofText = 'x';
  96.             char nextchar = 'x';
  97.             char firstCharacter = 'x';
  98.             String finalText = "";
  99.             String jVA = "JnVnA";
  100.             String placement = "";
  101.             boolean modified = false;
  102.             boolean discordSyntax = false;
  103.             boolean underscore = false;
  104.             boolean replaceNextChar = false;
  105.             boolean firstCharDecided = false;
  106.             //setting Variables
  107.  
  108.             Random random2 = new Random();
  109.             int idPlacement = random2.nextInt(9);
  110.             idPlacement++;
  111.             //Decides where the reference digit is placed
  112.             if (idPlacement == 6) {
  113.  
  114.                 Random randomDecider = new Random();
  115.                 int decisionMaker = randomDecider.nextInt(2);
  116.                 decisionMaker++;
  117.  
  118.                 if(decisionMaker == 1) {
  119.                     idPlacement = idPlacement + 1;
  120.                 } else {
  121.                     idPlacement = idPlacement - 1;
  122.                 }
  123.  
  124.             }
  125.             //Makes sure idPlacement is not 6, so there can be a definite < 6 or > 6
  126.             //Setting Identifier Placement [idPlacement]
  127.  
  128.  
  129.             char setIdentifyer = text.charAt(txtlength - 4);
  130.             if (setIdentifyer <= 6) {
  131.                 //If setIdentifyer is 1-5 then this digit is the identifier
  132.             }
  133.             if (setIdentifyer == 7) {
  134.                 //If setIdentifyer is 7 then identifier is at the front
  135.                 setIdentifyer = text.charAt(0);
  136.             }
  137.             if (setIdentifyer == 8) {
  138.                 //If setIdentifyer is 8 then identifier is at the front
  139.                 setIdentifyer = text.charAt(0);
  140.             }
  141.             if (setIdentifyer == 9) {
  142.                 //If setIdentifyer is 9 then identifier is at the front
  143.                 setIdentifyer = text.charAt(0);
  144.             }
  145.             //Sets the identifier based on a specific placement: front or back(before JVA)
  146.  
  147.  
  148.  
  149.  
  150.             Random rand = new Random();
  151.             int random = rand.nextInt(5);
  152.             random++;
  153.  
  154.  
  155.             if (setIdentifyer == '6') {
  156.                 modified = true;
  157.             }
  158.             if (setIdentifyer == '7') {
  159.                 modified = true;
  160.             }
  161.             if (setIdentifyer == '8') {
  162.                 modified = true;
  163.             }//Determines if text is modified or not
  164.  
  165.             char lastChar = text.charAt(txtlength - 1);
  166.             char seclastChar = text.charAt(txtlength - 2);
  167.             char thilastChar = text.charAt(txtlength - 3);
  168.             char firstChar = text.charAt(0);
  169.             String last3Char = thilastChar + "n" + seclastChar + "n" + lastChar;
  170.             System.out.println(last3Char);
  171.  
  172.  
  173.             if (last3Char.equals(jVA)) {//Determines whether to encode or decode
  174.  
  175.                 //decode
  176.                 System.out.println("decode");
  177.                 System.out.println(setIdentifyer);
  178.  
  179.  
  180.                 //setIdentifyer's placement is already determined by < or > if statements above
  181.                 if (setIdentifyer == '1') {
  182.                     //do nothing, maybe add something later?
  183.                     System.out.println("identifyer has been recieved for 1");
  184.                 }
  185.                 if (setIdentifyer == '2') {
  186.                     referance2 = referanceA1;
  187.                     System.out.println("identifyer has been recieved for 2");
  188.                 }
  189.                 if (setIdentifyer == '3') {
  190.                     referance2 = referanceA2;
  191.                     System.out.println("identifyer has been recieved for 3");
  192.                 }
  193.                 if (setIdentifyer == '4') {
  194.                     referance2 = referanceA3;
  195.                     System.out.println("identifyer has been recieved for 4");
  196.                 }
  197.                 if (setIdentifyer == '5') {
  198.                     referance2 = referanceA4;
  199.                     System.out.println("identifyer has been recieved for 5");
  200.                 }
  201.  
  202.                 if (setIdentifyer == '6') {
  203.  
  204.                     if (firstChar == '1') {
  205.                         //do nothing, maybe add something later?
  206.                         System.out.println("identifyer has been recieved for 1");
  207.                     }
  208.                     if (firstChar == '2') {
  209.                         referance2 = referanceA1;
  210.                         System.out.println("identifyer has been recieved for 2");
  211.                     }
  212.                     if (firstChar == '3') {
  213.                         referance2 = referanceA2;
  214.                         System.out.println("identifyer has been recieved for 3");
  215.                     }
  216.                     if (firstChar == '4') {
  217.                         referance2 = referanceA3;
  218.                         System.out.println("identifyer has been recieved for 4");
  219.                     }
  220.                     if (firstChar == '5') {
  221.                         referance2 = referanceA4;
  222.                         System.out.println("identifyer has been recieved for 5");
  223.                     }
  224.  
  225.                 }
  226.                 if (setIdentifyer == '7') {
  227.  
  228.                     if (firstChar == '1') {
  229.                         //do nothing, maybe add something later?
  230.                         System.out.println("identifyer has been recieved for 1");
  231.                     }
  232.                     if (firstChar == '2') {
  233.                         referance2 = referanceA1;
  234.                         System.out.println("identifyer has been recieved for 2");
  235.                     }
  236.                     if (firstChar == '3') {
  237.                         referance2 = referanceA2;
  238.                         System.out.println("identifyer has been recieved for 3");
  239.                     }
  240.                     if (firstChar == '4') {
  241.                         referance2 = referanceA3;
  242.                         System.out.println("identifyer has been recieved for 4");
  243.                     }
  244.                     if (firstChar == '5') {
  245.                         referance2 = referanceA4;
  246.                         System.out.println("identifyer has been recieved for 5");
  247.                     }
  248.  
  249.                 }
  250.                 if (setIdentifyer == '8') {
  251.  
  252.                     if (firstChar == '1') {
  253.                         //do nothing, maybe add something later?
  254.                         System.out.println("identifyer has been recieved for 1");
  255.                     }
  256.                     if (firstChar == '2') {
  257.                         referance2 = referanceA1;
  258.                         System.out.println("identifyer has been recieved for 2");
  259.                     }
  260.                     if (firstChar == '3') {
  261.                         referance2 = referanceA2;
  262.                         System.out.println("identifyer has been recieved for 3");
  263.                     }
  264.                     if (firstChar == '4') {
  265.                         referance2 = referanceA3;
  266.                         System.out.println("identifyer has been recieved for 4");
  267.                     }
  268.                     if (firstChar == '5') {
  269.                         referance2 = referanceA4;
  270.                         System.out.println("identifyer has been recieved for 5");
  271.                     }
  272.  
  273.                 }
  274.                 if (setIdentifyer == '9') {
  275.  
  276.                     if (firstChar == '1') {
  277.                         //do nothing, maybe add something later?
  278.                         System.out.println("identifyer has been recieved for 1");
  279.                     }
  280.                     if (firstChar == '2') {
  281.                         referance2 = referanceA1;
  282.                         System.out.println("identifyer has been recieved for 2");
  283.                     }
  284.                     if (firstChar == '3') {
  285.                         referance2 = referanceA2;
  286.                         System.out.println("identifyer has been recieved for 3");
  287.                     }
  288.                     if (firstChar == '4') {
  289.                         referance2 = referanceA3;
  290.                         System.out.println("identifyer has been recieved for 4");
  291.                     }
  292.                     if (firstChar == '5') {
  293.                         referance2 = referanceA4;
  294.                         System.out.println("identifyer has been recieved for 5");
  295.                     }
  296.                     clock++;
  297.                     clock++;//To avoid an unknown error that does not erase the front of the word
  298.  
  299.                 }
  300.                 //Changes referance2 based on setIdentifyer
  301.                 if (modified == true) {
  302.                     String placeHolder = "";
  303.                     int clock4 = 1;
  304.                     while (clock4 <= text.length() - 1) {
  305.                         placeHolder = placeHolder + text.charAt(clock4);
  306.                         clock4++;
  307.                     }
  308.                     System.out.println(placeHolder + "");
  309.                     text = placeHolder;
  310.                     txtlengthMinusOne = txtlengthMinusOne - 1;
  311.                 }
  312.  
  313.                 while (clock <= txtlengthMinusOne) {//Loop as many times as the length of your input
  314.  
  315.  
  316.                     clock2 = 0;
  317.                     System.out.println(text.charAt(clock));
  318.                     char origchar = text.charAt(clock);
  319.  
  320.                     if (clock <= text.length() - 2) {//If this isn't the last loop
  321.                         nextorigchar = text.charAt(clock + 1);
  322.                     }
  323.  
  324.                     while (clock2 <= reflengthMinusone) {//Loop as many times as the length of referance1
  325.  
  326.                         if (referance2.charAt(clock2) == origchar) {
  327.                             //If the character at clock2 of referance1 = the character being worked with
  328.  
  329.                             System.out.println(referance2.charAt(clock2));
  330.                             char newchar = referance1.charAt(clock2);
  331.                             //Locate and print the changed character
  332.  
  333.  
  334.  
  335.                             finalText = finalText + newchar;//Splice everything together
  336.  
  337.  
  338.                         }
  339.  
  340.                         clock2++;
  341.                     }
  342.  
  343.                     clock++;
  344.                 }
  345.  
  346.                 //Getting rid of "JVA"
  347.                 int clock3;
  348.                 if (modified == true) {
  349.                     clock3 = 1;
  350.                 } else {
  351.                     clock3 = 0;
  352.                 }
  353.  
  354.                 int finalLength = finalText.length() - 5;
  355.                 String finalText2 = "";
  356.                 while (clock3 <= finalLength) {
  357.  
  358.                     finalText2 = finalText2 + finalText.charAt(clock3);
  359.  
  360.                     clock3++;
  361.                 }
  362.  
  363.                 text = finalText2;
  364.                 clock = 0;
  365.                 firstCharofText = text.charAt(0);
  366.                 finalText = "";
  367.                 while (clock <= text.length() - 1) {
  368.                     if (text.charAt(clock) == ';') {
  369.                         if (text.charAt(clock + 1) == firstCharofText) {
  370.                             nextchar = ' ';
  371.                             finalText = finalText + nextchar;
  372.                             clock++;
  373.                         }
  374.                     } else {
  375.                         finalText = finalText + text.charAt(clock);
  376.                     }
  377.                     clock++;
  378.                 }
  379.  
  380.  
  381.                 if (finalText.length() == 4) {
  382.                     if (finalText.charAt(0) == '_') {
  383.                         underscore = true;
  384.                     }
  385.                 }//Detects underscores
  386.  
  387.                 //Removing underscores
  388.                 if (underscore == true) {
  389.                     clock2 = 0;
  390.                     while (clock2 <= 3) {
  391.                         firstCharacter = finalText.charAt(0);
  392.                         if (firstCharacter == '_') {
  393.                             System.out.println("Underscore found");
  394.                             text = finalText;
  395.                             finalText = "";
  396.                             clock = 1;
  397.                             while (clock <= text.length() - 1) {
  398.                                 finalText = finalText + text.charAt(clock);
  399.                                 clock++;
  400.                             }
  401.                         }
  402.                         clock2++;
  403.                     }
  404.  
  405.                 }
  406.                 System.out.println("");
  407.                 System.out.println("");
  408.  
  409.                 //Printing Stylishly
  410.                 clock = 0;
  411.                 while (clock <= finalText.length() - 1) {
  412.  
  413.                     if (finalText.charAt(clock) == '[') {
  414.                         if (finalText.charAt(clock + 1) == 'E') {
  415.                             if (finalText.charAt(clock + 2) == ']') {
  416.                                 if (finalText.charAt(clock + 3) == ' ') {
  417.                                     //try {
  418.                                     //Thread.sleep(50);
  419.                                     //} catch (InterruptedException e) {
  420.                                     // TODO Auto-generated catch block
  421.                                     //e.printStackTrace();
  422.                                     //}
  423.                                     System.out.print(finalText.charAt(clock));
  424.                                     clock++;
  425.                                 } else {
  426.  
  427.                                     //try {
  428.                                     //Thread.sleep(1200);
  429.                                     //} catch (InterruptedException e) {
  430.                                     // TODO Auto-generated catch block
  431.                                     //e.printStackTrace();
  432.                                     //}
  433.                                     clock += 3;
  434.  
  435.                                     if (finalText.charAt(clock) == ' ') {
  436.                                         clock++;
  437.                                     }
  438.  
  439.                                     System.out.println();
  440.                                 }
  441.                             }
  442.                         }
  443.  
  444.                     } else {
  445.                         //try {
  446.                         //Thread.sleep(50);
  447.                         //} catch (InterruptedException e) {
  448.                         // TODO Auto-generated catch block
  449.                         //e.printStackTrace();
  450.                         //}
  451.                         System.out.print(finalText.charAt(clock));
  452.                         clock++;
  453.                     }
  454.  
  455.  
  456.                 }
  457.  
  458.                 System.out.println("");
  459.                 System.out.println("");
  460.                 System.out.println("");
  461.                 output = finalText;
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.                 //end of decode
  470.             } else {
  471.  
  472.                 //encode
  473.                 if (idPlacement <= 6) {//Only if idPlacemant is below 6 (1-5)
  474.  
  475.                     if (idPlacement == 1) {
  476.                         //do nothing, maybe add something later?
  477.                     }
  478.                     if (idPlacement == 2) {
  479.                         referance2 = referanceA1;
  480.                     }
  481.                     if (idPlacement == 3) {
  482.                         referance2 = referanceA2;
  483.                     }
  484.                     if (idPlacement == 4) {
  485.                         referance2 = referanceA3;
  486.                     }
  487.                     if (idPlacement == 5) {
  488.                         referance2 = referanceA4;
  489.                     }
  490.                     //Changes referance2 to a different set basted on the random integer
  491.                     //regular
  492.                     //1,2,3,4,5
  493.  
  494.                     placement = "end";
  495.  
  496.                 } else {
  497.                     //Uses integer "random"
  498.  
  499.                     if (random == 1) {
  500.                         //do nothing, maybe add something later?
  501.                     }
  502.                     if (random == 2) {
  503.                         referance2 = referanceA1;
  504.                     }
  505.                     if (random == 3) {
  506.                         referance2 = referanceA2;
  507.                     }
  508.                     if (random == 4) {
  509.                         referance2 = referanceA3;
  510.                     }
  511.                     if (random == 5) {
  512.                         referance2 = referanceA4;
  513.                     }
  514.                     //Changes referance2 to a different set basted on the random integer
  515.                     //modified
  516.                     //7,8,9
  517.  
  518.                     placement = "beginning";
  519.  
  520.                 }
  521.  
  522.  
  523.  
  524.                 while (clock <= txtlengthMinusOne) {//Loop as many times as the length of your input
  525.  
  526.  
  527.                     clock2 = 0;
  528.                     System.out.println(text.charAt(clock));
  529.                     char origchar = text.charAt(clock);
  530.  
  531.                     if (replaceNextChar == true) {//replacing character
  532.                         origchar = text.charAt(0);
  533.                         replaceNextChar = false;
  534.                     }
  535.  
  536.                     if (clock <= text.length() - 2) {//replacing second space character
  537.                         nextchar = text.charAt(clock + 1);
  538.                         if (origchar == ';') {
  539.                             if (nextchar == ';') {
  540.                                 //replace next character with the third letter in the original text
  541.                                 replaceNextChar = true;
  542.                             }
  543.                         }
  544.                     }
  545.  
  546.                     while (clock2 <= reflengthMinusone) {//Loop as many times as the length of referance1
  547.  
  548.                         if (referance1.charAt(clock2) == origchar) {
  549.                             //If the character at clock2 of referance1 = the character being worked with
  550.  
  551.  
  552.                             System.out.println(referance2.charAt(clock2));
  553.                             char newchar = referance2.charAt(clock2);
  554.                             //Locate and print the changed character
  555.  
  556.                             finalText = finalText + newchar;//Splice everything together
  557.  
  558.                             if (firstCharDecided == false) {
  559.                                 firstCharofText = newchar;
  560.                                 firstCharDecided = true;
  561.                             }//Only looped once! Sets first character as firstChar
  562.  
  563.                         }
  564.  
  565.                         clock2++;
  566.                     }
  567.  
  568.                     clock++;
  569.                 }
  570.  
  571.  
  572.  
  573.                 if (placement.equals("end")) {//Normal placement [end]
  574.                     finalText = finalText + "" + idPlacement;
  575.                 }
  576.                 if (placement.equals("beginning")) {//Modified placement [beginning with idPlacement at end]
  577.                     finalText = random + "a" + finalText + idPlacement;
  578.                 }
  579.  
  580.  
  581.  
  582.                 finalText = finalText + "JVA";
  583.  
  584.                 //Avoiding Discord Syntax
  585.                 clock = 0;
  586.                 while (clock <= finalText.length() - 1) {
  587.  
  588.                     if (finalText.charAt(clock) == '*') {
  589.  
  590.                         charAst++;
  591.                         System.out.println(charAst + " *s found");
  592.  
  593.                     }
  594.                     if (finalText.charAt(clock) == '_') {
  595.  
  596.                         charUnd++;
  597.                         System.out.println(charUnd + " _s found");
  598.  
  599.                     }
  600.                     if (finalText.charAt(clock) == '~') {
  601.  
  602.                         charTil++;
  603.                         System.out.println(charTil + " ~s found");
  604.  
  605.                     }
  606.                     if (finalText.charAt(clock) == '`') {
  607.  
  608.                         charTil2++;
  609.                         System.out.println(charTil2 + " `s found");
  610.  
  611.                     }
  612.  
  613.                     clock++;
  614.  
  615.                 }
  616.  
  617.                 if (charAst >= 1) {
  618.                     discordSyntax = true;
  619.                 }
  620.                 if (charUnd >= 1) {
  621.                     discordSyntax = true;
  622.                 }
  623.                 if (charTil >= 1) {
  624.                     discordSyntax = true;
  625.                 }
  626.                 if (charTil2 >= 1) {
  627.                     discordSyntax = true;
  628.                 }//Checking for amount discord syntax may be used
  629.  
  630.  
  631.                 if (timesLooped == 100) {
  632.                     containsDiscordSyntax = true;
  633.                 }
  634.  
  635.                 if (discordSyntax == true) {
  636.                     System.out.println("Error: Output met Discord Syntax!");
  637.                     if (containsDiscordSyntax == false) {
  638.                         disSyn++;
  639.                     }//Stops when program "Gives Up"
  640.                     System.out.println("Error: Discord Syntax could not be avoided!");
  641.                     System.out.println("Warning! Your message contains Discord Syntax!");
  642.                     System.out.println("");
  643.                     System.out.println(finalText);
  644.                     System.out.println("");
  645.                     System.out.println("");
  646.                     System.out.println("");
  647.                 } else {
  648.                     System.out.println("");
  649.                     System.out.println(finalText);
  650.                     System.out.println("");
  651.                     System.out.println("");
  652.                     System.out.println("");
  653.                     //Print final encoded text
  654.                 }
  655.                 output = finalText;
  656.  
  657.  
  658.             }//encode
  659.  
  660.             timesLooped++;//to avoid an endless loop
  661.  
  662.         }//Loop
  663.  
  664.         scanner.close();
  665.     }//PSVM
  666.     public static void Display() {
  667.         txtChat.setText("");
  668.         screen = NW + " | " + N + " | " + NE + "\n";
  669.         txtChat.append(screen);
  670.         screen = W + " | " + M + " | " + E + "\n";
  671.         txtChat.append(screen);
  672.         screen = SW + " | " + S + " | " + SE + "\n";
  673.         txtChat.append(screen);
  674.     }
  675.     public static void ReadAndSet() {
  676.        
  677.         spaceTaken = false;
  678.        
  679.         if (uText.contains("NW")) {
  680.             char character = side.charAt(0);
  681.             if (NWtaken == true) {
  682.                 spaceTaken = true;
  683.             } else {
  684.                 Replace("NW", character);
  685.                 NWtaken = true;
  686.             }
  687.         } else if (uText.contains("NE")) {
  688.             char character = side.charAt(0);
  689.             if (NEtaken == true) {
  690.                 spaceTaken = true;
  691.             } else {
  692.                 Replace("NE", character);
  693.                 NEtaken = true;
  694.             }
  695.         } else if (uText.contains("SW")) {
  696.             char character = side.charAt(0);
  697.             if (SWtaken == true) {
  698.                 spaceTaken = true;
  699.             } else {
  700.                 Replace("SW", character);
  701.                 SWtaken = true;
  702.             }
  703.         } else if (uText.contains("SE")) {
  704.             char character = side.charAt(0);
  705.             if (SEtaken == true) {
  706.                 spaceTaken = true;
  707.             } else {
  708.                 Replace("SE", character);
  709.                 SEtaken = true;
  710.             }
  711.         } else if (uText.contains("N")) {
  712.             char character = side.charAt(0);
  713.             if (Ntaken == true) {
  714.                 spaceTaken = true;
  715.             } else {
  716.                 Replace("N", character);
  717.                 Ntaken = true;
  718.             }
  719.         } else if (uText.contains("E")) {
  720.             char character = side.charAt(0);
  721.             if (Etaken == true) {
  722.                 spaceTaken = true;
  723.             } else {
  724.                 Replace("E", character);
  725.                 Etaken = true;
  726.             }
  727.         } else if (uText.contains("S")) {
  728.             char character = side.charAt(0);
  729.             if (Staken == true) {
  730.                 spaceTaken = true;
  731.             } else {
  732.                 Replace("S", character);
  733.                 Staken = true;
  734.             }
  735.         } else if (uText.contains("W")) {
  736.             char character = side.charAt(0);
  737.             if (Wtaken == true) {
  738.                 spaceTaken = true;
  739.             } else {
  740.                 Replace("W", character);
  741.                 Wtaken = true;
  742.             }
  743.         } else if (uText.contains("M")) {
  744.             char character = side.charAt(0);
  745.             if (Mtaken == true) {
  746.                 spaceTaken = true;
  747.             } else {
  748.                 Replace("M", character);
  749.                 Mtaken = true;
  750.             }
  751.         }
  752.  
  753.         Display();
  754.         status = "won";
  755.         ValidateWin();
  756.         if (spaceTaken == true) {
  757.             Say(2, "Sorry that is already taken!");
  758.         }
  759.  
  760.     }
  761.     public static void Replace(String location, char letter) {
  762.         if (location.equals("NW")) {
  763.             NW = letter;
  764.             NWtaken = true;
  765.         } else if (location.equals("N")) {
  766.             N = letter;
  767.             Ntaken = true;
  768.         } else if (location.equals("NE")) {
  769.             NE = letter;
  770.             NEtaken = true;
  771.         } else if (location.equals("W")) {
  772.             W = letter;
  773.             Wtaken = true;
  774.         } else if (location.equals("M")) {
  775.             M = letter;
  776.             Mtaken = true;
  777.         } else if (location.equals("E")) {
  778.             E = letter;
  779.             Etaken = true;
  780.         } else if (location.equals("SW")) {
  781.             SW = letter;
  782.             SWtaken = true;
  783.         } else if (location.equals("S")) {
  784.             S = letter;
  785.             Staken = true;
  786.         } else if (location.equals("SE")) {
  787.             SE = letter;
  788.             SEtaken = true;
  789.         }
  790.     }
  791.     public static void Encode(String text) {
  792.         String text2 = "";
  793.         int clock = 0;
  794.         int textLength = text.length();
  795.         while (clock < textLength) {
  796.             if (text.charAt(clock) == ' ') {
  797.                 text2 = text2 + ";;";
  798.             } else {
  799.                 text2 = text2 + text.charAt(clock);
  800.             }
  801.             clock++;
  802.         }
  803.         Encoder(text2);
  804.     }
  805.     public static void GetBoard() {
  806.         board = NW + "" + N + "" + NE + "" + W + "" + M + "" + E + "" + SW + "" + S + "" + SE;
  807.     }
  808.     public static void ChangeBoard() {
  809.         int changed = 0;
  810.         char placeHolder;
  811.  
  812.         placeHolder = SE;
  813.         SE = output.charAt(output.length() - 1);
  814.         if (placeHolder != SE)
  815.             changed++;
  816.         SEtaken = true;
  817.  
  818.         placeHolder = S;
  819.         S = output.charAt(output.length() - 2);
  820.         if (placeHolder != S)
  821.             changed++;
  822.         Staken = true;
  823.  
  824.         placeHolder = SW;
  825.         SW = output.charAt(output.length() - 3);
  826.         if (placeHolder != SW)
  827.             changed++;
  828.         SWtaken = true;
  829.  
  830.         placeHolder = E;
  831.         E = output.charAt(output.length() - 4);
  832.         if (placeHolder != E)
  833.             changed++;
  834.         Etaken = true;
  835.  
  836.         placeHolder = M;
  837.         M = output.charAt(output.length() - 5);
  838.         if (placeHolder != M)
  839.             changed++;
  840.         Mtaken = true;
  841.  
  842.         placeHolder = W;
  843.         W = output.charAt(output.length() - 6);
  844.         if (placeHolder != W)
  845.             changed++;
  846.         Wtaken = true;
  847.  
  848.         placeHolder = NE;
  849.         NE = output.charAt(output.length() - 7);
  850.         if (placeHolder != NE)
  851.             changed++;
  852.         NEtaken = true;
  853.  
  854.         placeHolder = N;
  855.         N = output.charAt(output.length() - 8);
  856.         if (placeHolder != N)
  857.             changed++;
  858.         Ntaken = true;
  859.  
  860.         placeHolder = NW;
  861.         NW = output.charAt(output.length() - 9);
  862.         if (placeHolder != SW)
  863.             changed++;
  864.         NWtaken = true;
  865.  
  866.         Display();
  867.         if (changed > 1) {
  868.             Say(3, "Opponent Cheated! You win by default!");
  869.             Say(1, "Please send them this error!");
  870.             Encoder("You;;lose!;;You;;automatically;;lost;;for;;Cheating!");
  871.             Say(2, output);
  872.         }
  873.         if (changed == 0) {
  874.             Say(3, "Opponent did not make a move! Please send them this error!");
  875.             Encoder("Error:;;You;;did;;not;;make;;a;;move!;;Please;;try;;again!");
  876.             Say(2, output);
  877.         }
  878.         status = "lost";
  879.         ValidateWin();
  880.         if (scriptStopped == false) {
  881.             Say(2, "Your Turn!");
  882.         }
  883.     }
  884.     public static void Say(int entersBefore, String text) {
  885.         while (entersBefore != 0) {
  886.             txtChat.append("\n");
  887.             entersBefore--;
  888.         }
  889.         txtChat.append(text);
  890.     }
  891.     public static void Directions() {
  892.         txtChat.setText("");
  893.         screen = NW + " | " + N + " | " + NE + "\n";
  894.         txtChat.append(screen);
  895.         screen = W + " | " + M + " | " + E + "\n";
  896.         txtChat.append(screen);
  897.         screen = SW + " | " + S + " | " + SE + "\n";
  898.         txtChat.append(screen);
  899.         Say(2, "For location, use:");
  900.         Say(1, "NW, N, NE");
  901.         Say(1, "W, M, E");
  902.         Say(1, "SW, S SE");
  903.         Say(2, "Format:");
  904.         Say(1, "<Location>");
  905.         Say(2, "Type \"Directions\" to view this again!");
  906.     }
  907.     public static void PickSide() {
  908.         ClearScreen();
  909.         Say(3, "What side are you on (\"X\" or \"O\"): ");
  910.         txtEnter.setText("Side: ");
  911.         customTxtEnterText = "Side: ";
  912.     }
  913.     public static void Wait(String timeUnit, int time) {
  914.         int multiplier;
  915.         if (timeUnit.equals("s")) {
  916.             try {
  917.                 Thread.sleep(time);
  918.             } catch (InterruptedException e) {
  919.                 // TODO Auto-generated catch block
  920.                 e.printStackTrace();
  921.             }
  922.         } else if (timeUnit.equals("m")) {
  923.             multiplier = 60;
  924.             try {
  925.                 Thread.sleep(time * multiplier);
  926.             } catch (InterruptedException e) {
  927.                 // TODO Auto-generated catch block
  928.                 e.printStackTrace();
  929.             }
  930.         } else if (timeUnit.equals("h")) {
  931.             multiplier = 3600;
  932.             try {
  933.                 Thread.sleep(time * multiplier);
  934.             } catch (InterruptedException e) {
  935.                 // TODO Auto-generated catch block
  936.                 e.printStackTrace();
  937.             }
  938.         }
  939.     }
  940.     public static void ClearScreen() {
  941.         txtChat.setText("");
  942.     }
  943.     public static void ValidateWin() {
  944.         //__X_X_X__
  945.         if ((NE == M) && (M == SW)) {
  946.             if ((NE != '_') && (M != '_') && (SW != '_')) {
  947.                 win = true;
  948.                 winMethod = "__X_X_X__";
  949.             }
  950.         }
  951.  
  952.         //X___X___X
  953.         if ((NW == M) && (M == SE)) {
  954.             if ((NW != '_') && (M != '_') && (SE != '_')) {
  955.                 win = true;
  956.                 winMethod = "X___X___X";
  957.             }
  958.         }
  959.  
  960.         //XXX______
  961.         if ((NW == N) && (N == NE)) {
  962.             if ((NW != '_') && (N != '_') && (NE != '_')) {
  963.                 win = true;
  964.                 winMethod = "XXX______";
  965.             }
  966.         }
  967.  
  968.         //___XXX___
  969.         if ((W == M) && (M == E)) {
  970.             if ((W != '_') && (M != '_') && (E != '_')) {
  971.                 win = true;
  972.                 winMethod = "___XXX___";
  973.             }
  974.         }
  975.  
  976.         //______XXX
  977.         if ((SW == S) && (S == SE)) {
  978.             if ((SW != '_') && (S != '_') && (SE != '_')) {
  979.                 win = true;
  980.                 winMethod = "______XXX";
  981.             }
  982.         }
  983.  
  984.         //__X__X__X
  985.         if ((NE == E) && (E == SE)) {
  986.             if ((NE != '_') && (E != '_') && (SE != '_')) {
  987.                 win = true;
  988.                 winMethod = "__X__X__X";
  989.             }
  990.         }
  991.  
  992.         //_X__X__X_
  993.         if ((N == M) && (M == S)) {
  994.             if ((N != '_') && (M != '_') && (S != '_')) {
  995.                 win = true;
  996.                 winMethod = "_X__X__X_";
  997.             }
  998.         }
  999.  
  1000.         //X__X__X__
  1001.         if ((NW == W) && (W == SW)) {
  1002.             if ((NW != '_') && (W != '_') && (SW != '_')) {
  1003.                 win = true;
  1004.                 winMethod = "X__X__X__";
  1005.             }
  1006.         }
  1007.        
  1008.         EndGame();
  1009.  
  1010.     }
  1011.     public static void EndGame() {
  1012.         if (status.equals("won")) {
  1013.             if (win == true) {
  1014.                 System.out.println(winMethod);
  1015.                 Say(3, "You Won! Send the final code to your opponent!");
  1016.                 Say(2, "Thanks For playing!");
  1017.                 scriptStopped = true;
  1018.             }
  1019.         } else {
  1020.             if (win == true) {
  1021.                 System.out.println(winMethod);
  1022.                 Say(3, "You Lost!");
  1023.                 Say(2, "Thanks For playing!");
  1024.                 scriptStopped = true;
  1025.             }
  1026.         }
  1027.     }
  1028.  
  1029.  
  1030.     // Beginning of GUI Attributes:
  1031.  
  1032.  
  1033.  
  1034.  
  1035.     //Typing Area:
  1036.     private static JTextField txtEnter = new JTextField();
  1037.  
  1038.     //Chat Area:
  1039.     private static JTextArea txtChat = new JTextArea();
  1040.  
  1041.     public TicTacToe() throws InterruptedException{
  1042.         //Frame Attributes:
  1043.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  1044.         this.setSize(600, 600);
  1045.         this.setVisible(true);
  1046.         this.setResizable(false);
  1047.         this.setLayout(null);
  1048.         this.setTitle("TicTacToe [Java Edition]");
  1049.  
  1050.         //txtEnter Attributes:
  1051.         txtEnter.setLocation(2, 540);
  1052.         txtEnter.setSize(590, 30);
  1053.  
  1054.         //txtChat Attributes:
  1055.         txtChat.setLocation(15, 5);
  1056.         txtChat.setSize(560, 510);
  1057.         txtChat.setEditable(false);
  1058.  
  1059.         //Add Items To Frame:
  1060.         this.add(txtEnter);
  1061.         this.add(txtChat);
  1062.  
  1063.  
  1064.         //txtEnter Action Event:
  1065.         txtEnter.addActionListener(new ActionListener(){
  1066.             public void actionPerformed(ActionEvent arg0) {
  1067.  
  1068.  
  1069.                 uText = txtEnter.getText();
  1070.                 //txtChat.append(uText + "\n");
  1071.  
  1072.                 if ((uText.length() > 4) && (uText.charAt(uText.length() - 1) == 'A')) {
  1073.                     if ((uText.charAt(uText.length()-1) + "" + uText.charAt(uText.length() - 2) + "" + uText.charAt(uText.length() - 3)).equals("AVJ")) {
  1074.                         Encoder(uText);
  1075.                         if (output.charAt(output.length() - 1) == '!') {
  1076.                             Say(2, output);
  1077.                         } else
  1078.                             ChangeBoard();
  1079.                     }
  1080.                 } else if (uText.contains("Directions")) {
  1081.                     Directions();
  1082.                 } else if (uText.contains("Side: ")) {
  1083.                     if ((uText.charAt(uText.length() - 1) != 'X') && (uText.charAt(uText.length() - 1) != 'O')) {
  1084.                         Say(1, "Something went wrong! Please Try Again!");
  1085.                         PickSide();
  1086.                     } else {
  1087.                         customTxtEnterText = "";
  1088.                         side = uText.charAt(uText.length() - 1) + "";
  1089.                         Directions();
  1090.                     }
  1091.                 } else {
  1092.                     ReadAndSet();
  1093.                     GetBoard();
  1094.                     Encode(board);
  1095.                     if (scriptStopped == false) {
  1096.                         txtChat.append("\n \n \n \n" + output + "\n \nSend this to the opponent!");
  1097.                     }
  1098.                 }
  1099.  
  1100.                 txtEnter.setText(customTxtEnterText);
  1101.                 customTxtEnterText = "";
  1102.             }
  1103.  
  1104.         });
  1105.     }
  1106.  
  1107.  
  1108.  
  1109.     public static void main(String[] args) throws InterruptedException {
  1110.  
  1111.         PickSide();
  1112.         new TicTacToe();
  1113.  
  1114.     }
  1115.  
  1116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement