-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
80 lines (70 loc) · 2.03 KB
/
Main.java
File metadata and controls
80 lines (70 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
Title: Connect4.java
Author:Daniel Knoll
Design: A grid object with the following variables:
-2d Array of characters
methods:
turn_drop (char dropWhat, int colNum)):
-place a letter
'o' for player
'x' for computer
-finds the last possible cell in the column to sink to
display_grid:
-prints the 2d array in a pretty fashion
main:
variables:
-scanner user
-random gen
-instantiate grid
-while (true) loop
-display board
-wait for column selection for player turn
-player.didIWin
-computer decides randomly (unless time allows for making a smart computer)
-computer.didIWin
*/
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner user = new Scanner(System.in);
Random gen = new Random();
System.out.printf("Welcome to Connect-X!\nHow many columns would you like? (Connect4 uses 7) ");
int columns = user.nextInt();
System.out.printf("How many rows would you like? (Connect 4 uses 6) ");
int rows = user.nextInt();
System.out.println("You need 4 checkers to win."); // found variable checkers quantity not possible without some things i haven't learned yet
int checkers = 4;
GameGrid board = new GameGrid(columns, rows, checkers);
int colChoice;
board.display_grid();
while(true)
{
//player's turn
colChoice = 0;
do
{
System.out.printf("Your turn. Enter a column: 1-%d: ", board.columns);
colChoice = user.nextInt();
} while (colChoice < 1 || colChoice > board.columns);
board.turn_drop('O', colChoice - 1);
board.display_grid();
if(board.checkWin('O'))
{
System.out.println("You win!");
break;
}
//computer's turn
System.out.println("Computer's turn now ...");
colChoice = gen.nextInt(board.checkers);
board.turn_drop('X', colChoice);
board.display_grid();
if(board.checkWin('X'))
{
System.out.println("Computer wins!");
break;
}
}
}
}