► Beginner Java Programming - Episode 3: Tic Tac Toe

preview_player
Показать описание
In this episode we create the game Tic Tac Toe. We utilize all of the tricks we learned in the Java tutorial on Arrays & For Loops and cover Methods and return types.
Рекомендации по теме
Комментарии
Автор

i have been looking for java coding lessons on youtube for ages and you are my answer!! thank you and keep doing what you are doing!

TerraConn
Автор

You saved my sanity with this video! Thank you so much!  I am now a subscriber and more than likely a daily viewer of your posts.

kimberlybaker
Автор

for the support, within 1 or 2 more videos we will be starting a longer multi-video project that has a graphical component. My plan for that project is to split it up into smaller segments so that there is less code to digest at once, allowing more detailed definitions for each thing we do.  

As for making your own stuff my goal for these first videos is just to expose people to all of the basic tools like loops, if statements, variables, etc. that they can then use or modify to make their own programs.

CrossCoastGaming
Автор

Nice job and the logic is very well sorted. Congratulations

bacanalienigena
Автор

great videos! I will definitely continue with this series! very gratifying and clearly explained! Thanks.

bojiajie
Автор

I am liking these videos. My only comment would be to have a downloadable text file with the final product so we can compare our code when we have problems. I am having trouble with my code terminating without a chance to play and I have watched the video multiple times trying to compare my code with your. Thanks again for the education!

eloverphx
Автор

Hooray! I've been waiting for this!

DuskDesertdude
Автор

I'm not sure if anyone has said this yet, but seeing how you posted this a year ago I'm sure you've figured this out.  The easiest way to turn on line numbers is right click in the highlighted space (right to the left of your numbers..) and select "Show Line Numbers"! :) Much much easier and quicker than going through nav stuff. 

AMPnGUITAR
Автор

How do you print the condition "GAME OVER! IT IS A TIE " ?

harshchaudhari
Автор

Great video man. Thanks for the upload!

GMan
Автор

The game over Boolean is really confusing, can someone explain how it works?

geoffplays
Автор

Thank you so much! Saved me on my CS semester project

Zeptil
Автор

InfernityArchfiend11 You just need to remove the semicolon before the brackets for your "for statements". :)  Let me know if this helped.

CrossCoastGaming
Автор

So how would you write a condition that states if the player chooses a column or row outside of the predetermined range to "Please choose row and column between 1 and 3"?

beg
Автор

Sup man I realise I am quite late. Just wanted to ask you why in your for loops you aren't using i <= to whatever it is??

kelleno
Автор

i didn't get the rmove, cmove thing...why boolean is based on these and then you set the game over on the if/playing on row and col?

KiDMiO
Автор

if you really study through the code and read each method in order... this really is not too hard of a concept. Great tutorial and the code itself was clever as well. I just started learning from your videos but I did this as well as adding my own features to it :D I cannot figure out however how to stop people from entering things that are not integers... Here is my code:
package ubtuub;

import java.util.Scanner;

public class Main{
public static int row, col;
public static Scanner scan = new Scanner(System.in);
public static char[][] board = new char[3][3];
public static char turn = 'X';

public static void main(String[] args){
for(int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
board[i][j] = '_';
}
}
PrintBoard();
Play();
}

public static void Play(){
boolean playing = true;
while(playing){
System.out.println("Please enter a row 1-3 and press enter, followed by a column 1-3 and enter.");
row = scan.nextInt() - 1;
col = scan.nextInt() - 1;
while(validMove(row, col) == false){
System.out.println("Try again.");
PrintBoard();
row = scan.nextInt() - 1;
col = scan.nextInt() - 1;
}
board[row][col] = turn;

if(GameOver(row, col)){
playing = false;
System.out.print("Game over! Player " + turn + " has won!");
}else if(GameDrew()){
playing = false;
System.out.println("Game has ended in a draw!");

Play();
}else{
PrintBoard();
if(turn == 'X'){
turn = 'O';
}else{
turn = 'X';
}
}
}
}

public static void PrintBoard(){
System.out.print(" "+1+" "+2+" "+3);
for(int i = 0; i < 3; i++){
System.out.println();
for (int j = 0; j < 3; j++){
if(j == 0)System.out.print(i + 1 +" | ");
System.out.print(board[i][j] + " | ");
}
}
System.out.println();
}

public static boolean GameOver(int rMove, int cMove){
// Check straight victory
if(board[0][cMove] == board[1][cMove] && board[0][cMove] == board[2][cMove])return true;

if(board[rMove][0] == board[rMove][1] && board[rMove][0] == board[rMove][2]) return true;

// Check diagonal victory
if(board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[1][1] != '_') return true;

if(board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[1][1] != '_') return true;



return false;
}

public static boolean validMove(int rMove, int cMove){
if(rMove > 2 || rMove < 0 || cMove > 2 || cMove < 0){
System.out.println("Invalid space.");
return false;
}else if(board[rMove][cMove] != '_'){
System.out.println("Space taken.");
return false;
}

return true;
}
public static boolean GameDrew(){
int blanks = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(board[i][j] != '_'){
blanks++;
}
}
}
if(blanks == 9){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
board[i][j] = '_';
}
}
return true;
}
blanks = 0;
return false;
}

}

jimmytrollensky
Автор

How can I make the rows have number instead? For example from 1-9 ?

johanabrahamsen
Автор

What if the spots have been taken or the game is on a tie?

kiexen
Автор

the program works, but how do i save it such that it can be seen as a java application? n such that it can be fed into phones & actually be played?

kkm
welcome to shbcf.ru