package day053;
class Product{
String name;
int price;
}
public class Day05301클래스배열1 {
public static void main(String[] args) {
// 3칸짜리 배열이 2개 필요하면 2차원배열을 사용한다.
int[] arr = new int[3];
int [][] arr2 = new int[2][3];
//-------------------------------------------------
Product pr = new Product();
pr.name = "감자깡";
pr.price = 1000;
//-------------------------------------------------
// 마찬가지로 클래스도 배열로 필요하면 2차원배열처럼 만들어야한다.
Product [] prList = new Product[2];
prList[0] = new Product();
prList[1] = new Product();
prList[0].price = 10;
prList[1].price = 20;
prList[0].name = "새우깡";
prList[1].name = "고래밥";
}
}
package day053;
class Client{
String name;
int score;
// 인자를 저장하는 변수 "매개변수"(parameter)
void setData(String name , int score) {
this.name = name ;
this.score = score;
}
void printData() {
System.out.println(this.name + " : " + this.score);
}
}
public class Day05302클래스배열2 {
public static void main(String[] args) {
Client[] c = new Client[3];
for(int i=0; i<c.length; i++) {
c[i] = new Client();
}
// 들어가는 값을 "인자"(argument)
c[0].setData("김철수", 100);
c[1].setData("이만수", 20);
c[2].setData("박영희", 70);
for(int i=0; i<c.length; i++) {
c[i].printData();
}
}
}
package day053;
import java.util.Random;
class RanNum{
int num;
boolean check;
}
class DepolymentNum{
Random ran = new Random();
RanNum r = new RanNum();
void view(RanNum[]arr){
for (int i=0; i<arr.length; i++){
System.out.print(arr[i].num +" ");
}
System.out.println();
}
void NumChecker(){
RanNum[] arr = new RanNum[5];
for (int i=0; i<arr.length; i++){
arr[i]=new RanNum();
}
for (int i=0; i<arr.length; i++){
int r = ran.nextInt(5);
if(arr[r].check==false){
arr[r].check = true;
arr[i].num=r+1;
}else{
i--;
}
}
view(arr);
}
void run(){
NumChecker();
}
}
public class Day05303클래스배열중복숫자금지 {
public static void main(String[] args) {
DepolymentNum d = new DepolymentNum();
d.NumChecker();
}
}
package day053;
import java.util.Scanner;
class Seat{
boolean check;
void showData(){
if(check==true){
System.out.print("[o]");
}else{
System.out.print("[ ]");
}
}
}
class Reservation{
Scanner sc = new Scanner(System.in);
void menu(){
System.out.println("1.예매");
System.out.println("2.종료");
}
void ReservationCheck(Seat[] current){
System.out.println("예매할 자리를 선택하세요.");
int ch = sc.nextInt();
if(current[ch].check==false){
current[ch].check=true;
}else{
System.out.println("이미 예약된 좌석입니다.");
}
}
void run(Seat[] current){
while (true){
for (int i=0; i<current.length; i++){
current[i].showData();
}
System.out.println();
menu();
int num = sc.nextInt();
if(num==1) {
ReservationCheck(current);
}else{
break;
}
}
}
}
public class Day05304클래스배열자리예매 {
public static void main(String[] args) {
Reservation r = new Reservation();
Seat[] current = new Seat[8];
for (int i=0; i<current.length; i++){
current[i]=new Seat();
}
r.run(current);
}
}
package day053;
import java.util.Scanner;
class GameView {
int[] game = new int[3];
void view() {
for (int i = 0; i < game.length; i++) {
if (game[i] == 0) {
System.out.print("[ ]");
} else if (game[i] == 1) {
System.out.print("[o]");
} else if (game[i] == 2) {
System.out.print("[x]");
}
}
}
}
class GamePlay {
boolean turn = false;
int win;
GameView[] gv = new GameView[3];
void depolyment() {
for (int i = 0; i < 3; i++) {
gv[i] = new GameView();
}
}
void playCurrent() {
for (int i = 0; i < 3; i++) {
gv[i].view();
System.out.println();
}
}
void play() {
Scanner sc = new Scanner(System.in);
System.out.println("y");
int y = sc.nextInt();
System.out.println("x");
int x = sc.nextInt();
if(x<0 || y < 0 || x >= 3 || y >=3){
System.out.println("retry");
return;
}
if (turn == false && gv[y].game[x] == 0) {
gv[y].game[x] = 1;
turn = !turn;
} else if (turn == true && gv[y].game[x] == 0) {
gv[y].game[x] = 2;
turn = !turn;
} else {
System.out.println("retry");
}
}
void wiidthWinAndLose() {
for (int i = 0; i < gv.length; i++) {
int p1Cnt = 0;
int p2Cnt = 0;
for (int j = 0; j < gv[i].game.length; j++) {
if (gv[i].game[j] == 1) {
p1Cnt++;
} else if (gv[i].game[j] == 2) {
p2Cnt++;
}
}
if (p1Cnt >= 3) {
win = 1;
}
if (p2Cnt >= 3) {
win = 2;
}
}
}
void heightWinAndLose() {
for (int i = 0; i < gv.length; i++) {
int p1Cnt = 0;
int p2Cnt = 0;
for (int j = 0; j < gv[i].game.length; j++) {
if (gv[j].game[i] == 1) {
p1Cnt++;
} else if (gv[j].game[i] == 2) {
p2Cnt++;
}
}
if (p1Cnt >= 3) {
win = 1;
}
if (p2Cnt >= 3) {
win = 2;
}
}
}
void diagonalWinAndLose() {
int p1Cnt = 0;
int p2Cnt = 0;
for (int i = 0; i < 3; i++) {
if (gv[i].game[i] == 1) {
p1Cnt++;
} else if (gv[i].game[i] == 2) {
p2Cnt++;
}
}
}
void reversDiagonalWinAndLose() {
int p1Cnt = 0;
int p2Cnt = 0;
int x = 2;
for (int i = 0; i < 3; i++) {
if (gv[i].game[x - i] == 1) {
p1Cnt++;
} else if (gv[i].game[x - i] == 2) {
p2Cnt++;
}
}
}
void winner() {
if (win == 1) {
System.out.println("p1 Win");
} else {
System.out.println("p2 Win");
}
}
void run() {
depolyment();
while (true) {
playCurrent();
if (win != 0) {
winner();
break;
}
play();
wiidthWinAndLose();
heightWinAndLose();
diagonalWinAndLose();
reversDiagonalWinAndLose();
}
}
}
public class Day05305클래스배열틱택토 {
public static void main(String[] args) {
GamePlay gp = new GamePlay();
gp.run();
}
}
package day053;
import java.util.Random;
class Lotto {
int[] num = new int[8];
void lotto() {
for (int i = 0; i < num.length; i++) {
System.out.print(num[i] + " ");
}
System.out.println();
}
}
class DrawNumber {
Random ran = new Random();
Lotto[] lotto = new Lotto[5];
void depolyNum() {
for (int i = 0; i < lotto.length; i++) {
lotto[i] = new Lotto();
}
}
void prizLotto() {
for (int i = 0; i < lotto[0].num.length; i++) {
for (int j = 0; j < 8; j++) {
int r = ran.nextInt(2);
if (r == 0) {
lotto[0].num[j] = 0;
} else {
lotto[0].num[j] = 3;
}
}
int cnt = 0;
boolean t = false;
for (int j = 0; j < 8; j++) {
if (lotto[0].num[j] == 3) {
cnt++;
if (cnt == 3) {
t = true;
break;
}
} else {
cnt = 0;
}
}
if(t==false){
i--;
}
}
}
void OtherLottos() {
for (int i = 1; i < lotto.length; i++) {
for (int j = 0; j < 8; j++) {
int r = ran.nextInt(2);
if (r == 0) {
lotto[i].num[j] = 0;
} else {
lotto[i].num[j] = 3;
}
}
int cnt = 0;
boolean t = false;
for (int j = 0; j < 8; j++) {
if (lotto[i].num[j] == 3) {
cnt++;
if (cnt == 3) {
t = true;
break;
}
} else {
cnt = 0;
}
}
if(t==true){
i--;
}
}
}
void Suffle(){
for (int i=0; i<100; i++){
int r = ran.nextInt(5);
int[]tmp = lotto[r].num;
lotto[r].num = lotto[0].num;
lotto[0].num = tmp;
}
}
void Show() {
for (int i = 0; i < lotto.length; i++) {
for (int j = 0; j < lotto[i].num.length; j++) {
System.out.print(lotto[i].num[j] + " ");
}
System.out.println();
}
}
}
public class Day05306클래스배열로또한셋트 {
public static void main(String[] args) {
DrawNumber dn = new DrawNumber();
dn.depolyNum();
dn.prizLotto();
dn.OtherLottos();
dn.Suffle();
dn.Show();
}
}
import java.util.Random;
class Horse {
String name;
int pos;
int rank;
boolean win;
}
class Racing {
Random ran = new Random();
int size = 4;
int line = 20;
Horse[] horses = new Horse[size];
int[][] track = new int[size][line];
void run() {
String name = "ABCD";
for(int i=0; i<size; i++) {
horses[i] = new Horse();
horses[i].name = name.charAt(i) + "";
}
play();
printHorse();
}
void printHorse() {
System.out.println();
for(int i=0; i<size; i++) {
for(int j=0; j<line; j++) {
if(horses[i].pos == j) {
System.out.print("[" + horses[i].name + "]");
}else {
System.out.print("[ ]");
}
}
System.out.println();
}
System.out.println();
}
void play() {
int count = 0;
boolean loop = true;
while(loop) {
printHorse();
for(int i=0; i<size; i++) {
if(horses[i].win == true) continue;
int r = ran.nextInt(4) + 1;
horses[i].pos += r;
if(horses[i].pos >= 19) {
horses[i].pos = 19;
horses[i].rank = count + 1;
horses[i].win = true;
count = count + 1;
if(count >= size) {
loop = false;
break;
}
}
}
try {
Thread.sleep(500);
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
public class Day05307클래스배열경마게임 {
public static void main(String[] args) {
Racing race = new Racing();
race.run();
}
}
package day053;
import java.util.Scanner;
class User {
String id;
String pw;
int money;
void userPrint(){
System.out.println("아이디 : " + id + " 패스워드 : " + pw + " 잔액 : "+ money);
}
}
class UserManager{
User[] user;
int cnt = 0;
void print(){
for (int i=0; i<cnt; i++){
user[i].userPrint();
}
}
void addUser(String id, String pw){
if(cnt==0){
user = new User[cnt+1];
user[cnt] = new User();
user[cnt].id = id;
user[cnt].pw = pw;
user[cnt].money = 1000;
cnt++;
}else{
User[] tmp = user;
user = new User[cnt+1];
for (int i=0; i<cnt; i++) {
user[i] = new User();
user[i] = tmp[i];
}
user[cnt] = new User();
user[cnt].id = id;
user[cnt].pw = pw;
user[cnt].money = 1000;
cnt++;
}
}
int idCheck(String id){
int check = -1;
for (int i=0; i<cnt; i++){
if(user[i].id.equals(id)){
check = i;
}
}
return check;
}
void delUser(int check){
if(cnt==1){
user = null;
cnt--;
}else if(cnt>1){
User[]tmp = user;
user = new User[cnt-1];
int x =0;
for (int i=0; i<cnt; i++){
if(i!=check){
user[x] = tmp[i];
x++;
}
}
cnt--;
}
}
void run(){
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("[1]회원가입"); // id, pw, money(1000원)
System.out.println("[2]탈퇴");
System.out.println("[3]출력");
int choice = sc.nextInt();
if(choice==1){
System.out.println("원하는 id를 입력하세요.");
String id = sc.next();
idCheck(id);
if(idCheck(id)==-1){
System.out.println("비밀번호를 입력하세요.");
String pw = sc.next();
addUser(id,pw);
}else{
System.out.println("이미 존재하는 아이디입니다.");
}
}else if(choice==2){
System.out.println("원하는 id를 입력하세요.");
String id = sc.next();
idCheck(id);
if(idCheck(id)==-1){
System.out.println("없는 아이디입니다.");
}else {
System.out.println("비밀번호를 입력하세요.");
String pw = sc.next();
if(user[idCheck(id)].pw.equals(pw)){
System.out.println("탈퇴 완료");
delUser(idCheck(id));
}else{
System.out.println("비밀번호를 확인해주세요.");
}
}
}else if(choice==3){
print();
}
}
}
}
public class Day05308클래스배열회원가입 {
public static void main(String[] args) {
UserManager um = new UserManager();
um.run();
}
}
package day053;
import org.w3c.dom.ls.LSOutput;
import java.util.Random;
import java.util.Scanner;
class Word {
Random ran = new Random();
String word;
int ranPos;
void setRandomWordPos(String sample) {
word = sample;
int r = ran.nextInt(word.length());
ranPos = r;
}
void printWord() {
for (int i = 0; i < word.length(); i++) {
if (i == ranPos) {
System.out.print("*");
} else {
System.out.print(word.charAt(i));
}
}
System.out.println();
}
}
class WordSample {
Random ran = new Random();
String[] wordSampleList = {"java", "jsp", "python", "android", "spring"};
boolean[] checkList = new boolean[wordSampleList.length];
int cnt = wordSampleList.length;
String getRandomWord() {
int r = 0;
while (true) {
r = ran.nextInt(wordSampleList.length);
if (checkList[r] == false) {
checkList[r] = true;
cnt--;
break;
}
}
return wordSampleList[r];
}
}
public class Day05309클래스배열타자연습 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
WordSample wSample = new WordSample();
Word wordList = new Word();
boolean run = true;
long beforeTime = System.currentTimeMillis();
while (run) {
wordList.setRandomWordPos(wSample.getRandomWord());
while (true) {
wordList.printWord();
System.out.println("단어를 입력하세요.");
String inputWord = sc.next();
if (inputWord.equals(wordList.word)) {
System.out.println("정답");
break;
} else {
System.out.println("땡");
}
}
if (wSample.cnt == 0) {
long afterTime = System.currentTimeMillis();
double secDiffTime = (afterTime - beforeTime) / 1000.0;
System.out.println("기록 : " + secDiffTime + "초");
System.out.println("end");
break;
}
}
}
}